Question

I have a page that remembers the users name. But it tells me that there is an unexpected end of file. I looked for missing brackets, found one, added it, but now it tells me theres an unexpected bracket (by the session end). Any help???

<!DOCTYPE html>
<html>
<head>
<title>Remember UserName</title>
<?PHP
    session_start();
    if (isset($_POST['Submit1'])) {
        $username = $_POST['username'];

        if ($username == $_POST['username']) {
            print "Welcome back, ". " $username ";
            print "Would you like to log out?";
            echo '<form action="Logout" method="post">
            <Input Type = "Submit" Name = "Logout" Value = "Logout">
            </FORM>';

        }
        else {
            print ("You're not logged in to this site");
        }
    }

    if (isset($_POST['Logout'])) {
        $Logout = $_POST['Logout'];

        if ($Logout == $_POST['Logout']) 
            print "You are now logged out";
            session_destroy()
    }    
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="index.php">
<Input Type = "text" Value ="username" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Was it helpful?

Solution

This line:

session_destroy()
                 ^-- right there

is missing a closing semi-colon

session_destroy();

Which is the cause for the Unexpected End of File error error message.


Notes:

Using your present code, you may very well get the following error message: (which I tested)

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at...

It's best to use the following method, placing session_start(); above any output:

Sidenote: I am uncertain why you're using session_start(); in your code, since there are no session variables in your posted code. If you're not going to be using something to the effect of $username=$_SESSION['username']; then you can safely remove it.

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
<title>Remember UserName</title>
<?php
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];

if ($username == $_POST['username']) {
print "Welcome back, ". " $username ";
print "Would you like to log out?";
echo '<form action="Logout" method="post">
<Input Type = "Submit" Name = "Logout" Value = "Logout">
</FORM>';

}
else {
print ("You're not logged in to this site");
}
}

if (isset($_POST['Logout'])) {
$Logout = $_POST['Logout'];

if ($Logout == $_POST['Logout']) 
        print "You are now logged out";
        session_destroy();
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="index.php">
<Input Type = "text" Value ="username" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>

Additional notes:

I also noticed a few discrepencies in your logout button.

This echo '<form action="Logout" method="post">

should be replaced with echo '<form action="" method="post">

(unless you're using an actual file, yet this seems to be most unlikely)

Plus, you can narrow this down:

if (isset($_POST['Logout'])) {
    $Logout = $_POST['Logout'];

    if ($Logout == $_POST['Logout']) 
        print "You are now logged out";
        session_destroy();
} 

to:

if (isset($_POST['Logout'])) {
        print "You are now logged out";
        session_destroy();
}

Complete rewrite:

<?php
session_start();
?>

<!DOCTYPE html>
<html>
<head>
<title>Remember UserName</title>

<?php
if (isset($_POST['Submit1'])) {
    $username = $_POST['username'];

if ($username == $_POST['username']) {
    print "Welcome back, ". " $username ";
    print "Would you like to log out?";
    echo '<form action="" method="post">
        <Input Type = "Submit" Name = "Logout" Value = "Logout">
</form>';

}
else {
    print ("You're not logged in to this site");
    }
}

if (isset($_POST['Logout'])) {
    print "You are now logged out";
    session_destroy();
  }
?>
</head>
<body>
<form name ="form1" Method ="POST" Action ="">
<Input Type = "text" Value ="username" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</form>
</body>
</html>

OTHER TIPS

While not the explicit cause of your EOF error,

if (isset($_POST['Logout'])) {
    $Logout = $_POST['Logout'];          // Okay you assigned it...

    if ($Logout == $_POST['Logout'])     // ... and now you're seeing if they're equal?
    {                                    // You want braces here...
        print "You are now logged out";
        session_destroy();     // As Fred -ii- mentioned you're missing this ;
    }                                    // ...and here
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top