Wolverine68 Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148385-solved-problem-executing-logout-feature-in-a-session/ Share on other sites More sharing options...
wildteen88 Posted March 7, 2009 Share Posted March 7, 2009 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 https://forums.phpfreaks.com/topic/148385-solved-problem-executing-logout-feature-in-a-session/#findComment-779058 Share on other sites More sharing options...
Wolverine68 Posted March 7, 2009 Author Share Posted March 7, 2009 Thank you. Link to comment https://forums.phpfreaks.com/topic/148385-solved-problem-executing-logout-feature-in-a-session/#findComment-779092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.