Hi guys,
I'm currently in the process of creating a login form. I'm using PHP to check a simple text file called 'users.txt' for the username and password which has been entered in the form.
If the username and password are NOT in the 'users.txt' file, it will create them on a new line.
Like so:
Users.txt
ExampleUser,ExamplePass\n
Marc,password
Craig,password
John,password
Once I try to log into an account which is NOT there, it will create an account underneath. So if I try to log in with username as "Matthew" and password as "password" it will show like so:
ExampleUser,ExamplePass\n
Marc,password
Craig,password
John,password
Matthew,password
Hoping this makes sense so far, all of the above works.
However when I click back, to go back onto the login form, I try to log in with one of the usernames/passwords in the 'users.txt' file, and it will create the exact same user on a new line, so I have 2 of the same usernames/passwords.
What I want it to do it, if the username is in the 'users.txt' file, for it to display a message saying "Congratulations you're logged in".
Here is the code for the PHP login page.
P4 LoginScriptFile.php
<?php
//This checks for required fields from the form.
if ((!$_POST[username]) || (!$_POST[password]))
{
header("Location: P4 LoginForm.php");
exit;
}
//This reads values from the form.
$form_user = $_POST[username];
$form_password = $_POST[password];
$flag = FALSE;
$filename = "users.txt";
$fp = fopen( $filename, "r" ) or die ("Couldn't open $filename");
while ( ! feof( $fp ) ) {
$line = fgets( $fp);
$user = strtok($line, ","); //Username
$password = strtok(","); //Password
if (($form_user == $user) && ($form_password == $password))
{
$flag = TRUE;
}
}
if ($flag)
{
echo "<br>Congratulations, you're logged in";
}
else{
$filename = "users.txt";
$updateuser = $_POST ['username'];
$updatepass = $_POST ['password'];
$fp = fopen( $filename, "a" ) or die("Couldn't open $filename");
fwrite( $fp, "$updateuser,$updatepass\n") or die ("Couldn't write values to your file!");
fclose( $fp );
echo "<br>An account has been created for you!";
}
?>
I think what I need is to read the file once the new user has been created. Any help would be greatly appreciated.
Thanks in advance for any help.
gixxx