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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...