Chris is right this is a very old thread and that script as posted is buggier that a dead body in the swamp for a week.
If you still want to play try this:
Create an empty text file named users.txt
Copy this code to a file named signup.html
PHP Code:
<html>
<body>
<form action="register.php" method="post">
Username: <input type="text" name="username">
<br /><br />
Password: <input type="password" name="password">
<br /><br />
Confirm Password: <input type="password" name="password2">
<br /><br />
<input type="submit" name="submit" value="register">
</form>
</body></html>
Copy this code to a file named register.php
PHP Code:
<?php
//debug
echo "password1 ".$_POST['password'];
echo "<br>";
echo "password2 ".$_POST['password2'];
echo "<br><br>";
// end debug
if (isset ($_POST['submit'])) {
$problem = FALSE;
if (empty ($_POST['username'])) {
$problem = TRUE;
print "Please enter a username!";
}
if (empty ($_POST['password'])) {
print "Please go back and enter a password" ;
exit;
}
if (empty ($_POST['password2'])) {
print "Please go back enter your password in each box.";
exit;
}
if (($_POST['password'])!==($_POST['password2']))
{
print "Your password entries did not match! Please go back and correct them";
exit;
}
// First thing is to be sure that the usename is not in use by someone else
$fp = fopen ( 'users.txt', 'rb' );
while ($line = fgetcsv($fp,100,",")) {
echo $line[0]." ".$line[1];//debug ONLY! Allows you to see database contents.
echo "<br>"; //debug
if ( ($line[0] == $_POST['username']) ) {
echo "<b>Sorry! That username is already being used. Please go back and select another name.</b>";
fclose ( $fp );
exit;
}
}
// if there is no problem open the file and write to it
$fp=fopen('users.txt', 'ab' );
//$data =($_POST['username']).crypt($_POST['password']);
$data =($_POST['username']).",".($_POST['password'])."\r\n";
// Write data and close file!
fwrite ( $fp, $data );
fclose ( $fp );
print "You are now registered!";
}
exit;
?>
Copy this code to a file named login.php
PHP Code:
<?php
//debug
echo $_POST['username'];
echo "<br>";
echo $_POST['password'];
echo "<br>";
// end debug
if (isset ($_POST['submit'])) {
$loggedin = FALSE;
$fp = fopen ( 'users.txt', 'rb' );
while ($line = fgetcsv($fp,100,",")) {
echo $line[0]." ".$line[1];//debug ONLY! Allows you to see database contents.
echo "<br>"; //debug
if ( ($line[0] == $_POST['username']) AND ($line[1] == ($_POST['password']) ) ) {
$loggedin = TRUE;
echo "line 24 <br>"; //debug
//break;
}
}
if ($loggedin) {
print '<br>The password and username are verified.';
echo"<br><br>Now what do you want to do with that login?";
echo"<br><br> I am guessing that this is where you start learning all about sessions.";
echo "(evil grin)";
exit;
} else {
print '<br>The username and password did not match!';
}
}
?>
<form action="login.php" method="post">
Username: <input type="text" name="username">
<br /><br />
Password: <input type="password" name="password">
<br /><br />
<input type="submit" name="submit" value="Login">
</form>
You may have to chmod users.txt to 777 to get it to work but try the following first in this order:
666, 766
Default on most servers is 666. Running on localhost on your windows machine chmod is not necessary
Note the encryption has been removed until you finish playing and figure out how you are going to use this.
Have fun! This entertained me for a few hours on a rainy Saturday morning.
