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? Link to comment https://forums.phpfreaks.com/topic/120510-closing-a-session-from-a-link/ 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> Link to comment https://forums.phpfreaks.com/topic/120510-closing-a-session-from-a-link/#findComment-620988 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(); ?> Link to comment https://forums.phpfreaks.com/topic/120510-closing-a-session-from-a-link/#findComment-620989 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? Link to comment https://forums.phpfreaks.com/topic/120510-closing-a-session-from-a-link/#findComment-620996 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. Link to comment https://forums.phpfreaks.com/topic/120510-closing-a-session-from-a-link/#findComment-621012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.