Jump to content

[SOLVED] Problem executing logout feature in a session


Wolverine68

Recommended Posts

I posted a sessions topic earlier today where I had the user login with a name and password. If successful it created an active session and displayed the appropriate message. If not, it displayed an "access denied" message.

 

Now, I want to add a logout feature. If the user is successful in logging into a session, there will be a button they click on when they're ready to logout. After clicking the button, it takes them to a logout.php page. The session is destroyed and a message displayed saying they have logged out. 

 

Login.php page:

 

<?php
session_start();
$_SESSION['name'] = "ken";
$_SESSION['password'] = "welcome";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Session Variables</title>
</head>

<body>
<div>
<form action="display.php" method="POST">
<p>Enter your username and password to login:</p>
<p>Name:<INPUT TYPE="text" SIZE="20" name="name"></p>
<p>Password:<INPUT TYPE="text" SIZE="20" name="password"></p>
<p><INPUT TYPE="submit" VALUE="Submit!"></p>
</form>
</div>
</body>

</html>

 

Display.php page:

 

<?php
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Validating Session</title>
</head>

<body>
<div>
<?php

if (($_POST['name'] == $_SESSION['name']) && ($_POST['password'] == $_SESSION['password'])) {

$_SESSION['Active'] = "True";

}
else
{
$_SESSION['Active'] = "False";
}
?>

<?php
If ($_SESSION['Active'] == "True") {

echo "Welcome,  " .$_POST['name'].  "you have successfully logged into your session.";

}

else
{
echo "You have entered an invalid username and/or password. Access denied";
}
?>
<form action="logout.php" method="POST">
<p>Click on the following button when you want to logout of your session: <input type="submit" 

value="Logout"></p>
</form>

</div>
</body>
</html>

 

Logout.php page:

 

<?php
session_start();
unset($_SESSION['Active']);
session_destroy();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<title>Session Variables</title>
</head>

<body>
<div>
<p><?php echo "You have logged out of your session"; ?>
</div>
</body>

</html>


 

Even if the user entered a wrong name or password, they're still going to see the message "Click on the button to logout". Of course, I only want that message to appear if the login was successful, since they can't logout if they weren't successful in logging in to begin with.

 

Link to comment
Share on other sites

Change

<?php
If ($_SESSION['Active'] == "True") {

echo "Welcome,  " .$_POST['name'].  "you have successfully logged into your session.";

}

else
{
echo "You have entered an invalid username and/or password. Access denied";
}
?>
<form action="logout.php" method="POST">
<p>Click on the following button when you want to logout of your session: <input type="submit"

value="Logout"></p>
</form>

 

to

<?php
If ($_SESSION['Active'] == "True") {

echo "Welcome,  " .$_POST['name'].  "you have successfully logged into your session.";

echo '<form action="logout.php" method="POST">
<p>Click on the following button when you want to logout of your session: <input type="submit" value="Logout"></p>
</form>';'

}

else
{
echo "You have entered an invalid username and/or password. Access denied";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.