goocharlton Posted August 20, 2008 Share Posted August 20, 2008 I am trying to close a php session from a link that the user clicks. For example, I want the user's session to end when he clicks "(logout)". How do I do this? Quote Link to comment Share on other sites More sharing options...
abdfahim Posted August 20, 2008 Share Posted August 20, 2008 if(isset($_GET['logout'])){ $_SESSION=array(); // I always use this line for more security unset($_SESSION); // here you can use Header if you want to redirect the user to some other place } <a href="?logout=1">logout</a> Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 From the manual, the best way should be: <?php session_start(); $_SESSION = array(); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } session_destroy(); ?> Quote Link to comment Share on other sites More sharing options...
goocharlton Posted August 20, 2008 Author Share Posted August 20, 2008 Thanks guys. GuiltyGear could you please expand on your answer and give an explanation? Quote Link to comment Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 You can use it in the way suggested by abdbuet, meaning insert the code in an if() to check if logout isset(). <?php session_start(); if(isset($_GET['logout'])){ // Unset all of the session variables. $_SESSION = array(); // If it's desired to kill the session, also delete the session cookie. // Note: This will destroy the session, and not just the session data! if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } // Finally, destroy the session. session_destroy(); } ?> The comments are from the manual (in the session_destroy() function). What it does is simple, it destroys the session variable together with it's associated cookie. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.