PDA

View Full Version : Creating a login using CSV's



tech_support
11-23-2006, 05:52 AM
Is there a way to create a login system using a CSV table?

codeexploiter
11-23-2006, 06:08 AM
That can be done the file operations supported by the PHP without much difficulty.

I've done that once earlier but later changed that to database rather than using CSV.


The CSV will be encrypted

You mean the information stored in the CSV file or the file itself?

shachi
11-23-2006, 04:51 PM
I have tried it once too. It gives a lot of pain when you need to update the values.





The CSV will be encrypted


You mean the information stored in the CSV file or the file itself?


I don't think the OP ever posted that(at least I can't see that in the original post).

tech_support
11-23-2006, 10:16 PM
I've done that once earlier but later changed that to database rather than using CSV.


I have tried it once too. It gives a lot of pain when you need to update the values.

Can you give me the file? I could do it myself but I just haven't got the time.



You mean the information stored in the CSV file or the file itself?


The information stored in the CSV file.

shachi
11-24-2006, 06:19 PM
Ok, here it is. It needs a little bit of tweaking(sorry I am a PHP newbie).

login.php



<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?php
if(isset($_GET['login'])){
if(isset($_SESSION['logged'])){
echo 'You cannot login in when you are logged in already.';
} else {
?>
<form name="login" method="POST" action="processes.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit"><br>
<a href="?register=true">Please Register Here</a>
</form>
<?php
}
} else if(isset($_GET['register'])){
if(isset($_SESSION['logged'])){
echo 'You cannot register when you are logged in.';
} else {
?>
<form name="login" method="POST" action="register.php">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
<?php
}
} else {
if(isset($_SESSION['logged'])){
echo 'You don\'t have any previleges here.';
} else {
?>
Please <a href="?login=true">Login</a><br>
OR<br>
Register <a href="?register=true">here</a>
<?php
}
}
?>
</body>
</html>


processes.php



<?php

# function newfile, by Twey of dynamicdrive

function newfile($filename){
$lines = file($filename);
$config = array();
for($i = 1; $i < count($lines); ++$i)
$config[substr($lines[$i], 0, strpos($lines[$i], ':'))] = substr($lines[$i], strpos($lines[$i], ':') + 1, -1); // -2 for a Windows host
return $config;
}
if(isset($_GET['logout'])){
unset($_SESSION['logged']);
}
//print_r(newfile('users.php'));
$users = newfile('users.php');
//echo $test['test1'];
$username = $_POST['username'];
$password = $_POST['password'];
list($pass,$email) = explode(',', $users[$username]);
if(isset($users[$username]) && md5($password) == $pass){
session_start();
$_SESSION['logged'] = $username;
echo 'Hello you are registered.<br>';
echo 'Click <a href="somepage.php">here</a> to go to some other page.<br>';
echo 'Click <a href="processes.php?logout=true">here</a> to logout.<br>';
echo 'Your email address is '.$email;
} elseif(!isset($users[$username]) && md5($password) != $users[$username]) {
echo 'Username or password Incorrect';
}
if(isset($_SESSION['logged'])){
echo '<br>test';
}
?>



register.php



<?php
function newfile($filename){
$lines = file($filename);
$config = array();
for($i = 1; $i < count($lines); ++$i)
$config[substr($lines[$i], 0, strpos($lines[$i], ':'))] = substr($lines[$i], strpos($lines[$i], ':') + 1, -1); // -2 for a Windows host
return $config;
}
$users = newfile('users.php');
$user = $_POST['username'];
$pass = md5($_POST['password']);
$email = $_POST['email'];
if(isset($user) && isset($pass)){
if(array_key_exists($user, $users)){
echo 'Username Taken';
} else {
$f = fopen('users.php', 'a');
fwrite($f, $user.":".$pass.",".$email."\n");
fclose($f);
echo 'Successfully registered!!! Please click here to login.<br><a href="login.php?login=true">Login</a>';
}
} else {
echo 'No Username or password';
}
?>



somepage.php



<?php
session_start();
if(isset($_SESSION['logged'])){
echo 'You are still logged in';
echo '<br><a href="processes.php?logout=true">Logout</a>';
} else {
echo 'Please login first';
}
?>



users.php



<?php exit();?>


Hope it helps!!

tech_support
11-25-2006, 06:16 AM
Yep, That's exactly what I wanted. Thanks!

shachi
11-25-2006, 07:40 AM
You're welcome. By the way the newfile function is by Twey so I would be grateful if you could place the notice intact.

tech_support
12-01-2006, 07:20 AM
Just noticed that. Ok ;)

shachi
12-01-2006, 12:16 PM
Thanks a lot. :)