Jump to content
Larry Ullman's Book Forums

Need Help - Pg 328 (Script 11.8)


Recommended Posts

Thank you anybody/everybody in advance for your help here... 

 

Ok, I give up... Been staring at this for a while.  Was given this error message: 

Parse error: syntax error, unexpected $end in /Applications/XAMPP/xamppfiles/htdocs/Ch. Lessons/login.php on line 58

 

***Line 58 is the last line of the code.

 

I counted parenthesis and brackets and I believe everything matches up but I obviously have something wrong and its the first time I've hit a real brick wall in this book - I was hoping to get through it without any issues!! ;)

 

OS = OSX10.6.4

Browser = Firefox 22.0

 

 

Here is my code:

 

 

<!DOCTYPE HTML>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>

<?php //Script 11.8 - login.php
//This script logs a user in by checking the stored values in a txt file.
//Identify the file to use:
$file = '../users/users.txt';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {   //Handle the form
    
    $loggedin = FALSE;  //Not currently logged 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:
            break;
            
        }  //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 or password entered do not match those on file.</p>';
}
} else { //Display the form.
// Leave PHP and display the form.    
?> }  // End of submission IF.

<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>

</body>
</html>

Link to comment
Share on other sites

I think this line is causing your error

?> }  // End of submission IF.

You've closed the php tag before closing the else clause. It needs to be the other way round.

 }  // End of submission IF.
?>

Once you've been doing this for awhile, you spot these errors straight away. Also, you might want to consider using a good text editor which provides colour coding and these sorts of errors will be easier to find, particularly if you have long scripts. TextWrangler is pretty good and is free.

 

Please use code tags when you post code, it makes the post easier to read. Its the <> button on the editing bar.

 

Hope this helps.

  • Upvote 2
Link to comment
Share on other sites

I think this line is causing your error

?> }  // End of submission IF.

You've closed the php tag before closing the else clause. It needs to be the other way round.

 }  // End of submission IF.
?>

Once you've been doing this for awhile, you spot these errors straight away. Also, you might want to consider using a good text editor which provides colour coding and these sorts of errors will be easier to find, particularly if you have long scripts. TextWrangler is pretty good and is free.

 

Please use code tags when you post code, it makes the post easier to read. Its the <> button on the editing bar.

 

Hope this helps.

 

Thank you very much!  You are correct it worked and was a simple mistake (if you want to call it that).

 

I use a txt editor but it didn't color code it as if it were wrong which is interesting.

 

I will going forward use the

<>

so that its easier to read the code.

 

Thanks again!

Link to comment
Share on other sites

 

I use a txt editor but it didn't color code it as if it were wrong which is interesting.

The colour wont necessarily have changed when you closed the php tag. If the editor comes with code folding functionality, that can be a useful though subtle indicator to the cause of the error.

Link to comment
Share on other sites

Ahh..  got it!  Nice hint thank you very much!

 

I'll be wrapping up this book in the next week or so and then I'll be on to PHP and MySQL so I'm sure this won't be the last you hear from me! ;) 

 

Appreciate the help once again truly.. 

Link to comment
Share on other sites

Guest Deleted

Awesome. Finishing a book feels good. I'm hoping to finish Larry's advanced PHP book before too long. I'm over halfway now. So far, so good.

 

After I finish this I can focus more attention on learning JavaScript and jQuery. That should be exciting. (Not to say PHP isn't exciting, though I do admit I'm a little bored with it after using it for 10 years.)

Link to comment
Share on other sites

 Share

×
×
  • Create New...