Jump to content
Larry Ullman's Book Forums

Ch. 11 Login.php, Script 11.8

Recommended Posts

Hi everyone. I've done everything I could think of to get Script 11.8 to work, but I keep getting the error message 'The username and password you entered do not match those on file'. The users.txt is where it should be (because it worked fine for register.php) and the script is almost exactly how it is in the book. I'm using the same user names and passwords as well. Is this happening because I'm on a Mac? Oh, I'm on PHP 5.3.6 and use XAMPP.


<?php // Script 11.8 - login.php

$file = '../users/users.txt';

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
	$loggedin = FALSE; // Not currently signed in.
	// Enable auto_detect_line settings:
	ini_set('auto_detect_line_endings', 1);
	// Open the file:
	$fp = fopen($file, 'rb');
	// Loop through the file:
	while ( $line = fgetcsv($fp, 200, "\t") ) {
		// Check the file data against the submitted data:
		if ( ($line[0] == $_POST['username']) AND ($line[1] == md5(trim($_POST['password']))) ) {
			$loggedin = TRUE; // Correct username/password combination.
			// Stop looping through the file:
		} // End of IF.
	} // End of WHILE.
	fclose ($fp); // Close the file.
	// Print a message:
	if ($loggedin) {
		print '<p>You are now logged in.</p>';
	} else {
		print '<p style="color: red;">The username and password you entered do not match those on file.</p>';
} else { // Display the form.

// Leave PHP and display the form:

<form action="login.php" method="post">
<p>Username: <input type="text" name="username" size="20" /></p>
<p>Password: <input type="password" name="password" size="20" /></p>
<input type="submit" name="submit" value="Login" /></form>

<?php } // End of submission IF. ?>	


Link to comment
Share on other sites

Hello, and welcome to the forums.


Try echoing the input values and the values in the text file out to the screen, and see if they're the same.

Also, try checking the length of the strings with the strlen function, and see if they're the same.

There may be some trailing whitespace or something that's throwing you off.

Link to comment
Share on other sites

@HartleySan Thanks. I inserted both strlen and echo into login. php. The results showed the same thing as what was put in with register.php. There's nothing extra. This is the code I used:

print strlen($_POST['username'])."<br>";
print strlen(md5(trim($_POST['password'])))."<br>";
echo md5(trim($_POST['password']))."<br>";
echo $_POST['username'];
Link to comment
Share on other sites

How would I go about getting the values in the text file? I tried using this code in login.php,

echo nl2br(file_get_contents($file))

...but nothing happened. It returned a blank line where the output should have been.


Out of curiosity, I tried the code in register.php and it showed everything in the text file perfectly. I just don't get it. What is it about my login.php that's making it not acknowledge my text file at all?

Link to comment
Share on other sites

Ignoring nl2br for a second, if you execute the following statement and nothing is output, then that means your text file is empty (or contains only whitespace):

echo file_get_contents($file);


If that's the case, then I think it's pretty obvious why your script is failing, and you need to figure out why the text file is empty when it shouldn't be.

Link to comment
Share on other sites

OK, echo n12br(file_get_contents($file)) works for both of them now. Both login.php and register.php can display what's currently in the text file. One of the periods got lost in the third line. Must have accidentally deleted one somehow.


And now even better news. Login.php finally worked! It says "You are now logged in!" Not exactly sure what made it work (the period wasn't always a problem--see first post), but it works now.


Anyhow, thanks for the help HartleySan. Now I can happily continue on in the book  :)

Link to comment
Share on other sites


  • Create New...