Destramic Posted August 16, 2014 Share Posted August 16, 2014 hey guys im having a spot of botter unsetting, destroying a session which has been given a cookie lifetime...i know it is possible to destroy all sessions using session_destroy() but i want to destory a particials session and not all of them. here is my method if anyone can give some advice on how i could do this please...thank you public function destroy($name = null) { $namespace = $this->_namespace; $sessions = $_SESSION[$namespace]; if ($name == null) { foreach ($sessions as $name => $value) { $session = $sessions[$name]; unset($sesion); if (isset($session)) { // sessions with lifetime ini_set('session.cookie_lifetime', 0); ini_set('session.gc_max_lifetime', 0); ini_set('session.gc_probability', 1); ini_set('session.gc_divisor', 1); $session; } } } else { unset($sessions[$name]); } session_regenerate_id(true); } Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 16, 2014 Share Posted August 16, 2014 Your code has some sort of namespacing feature I don't understand. Any efforts you make to remove the session data is going to break that, so I don't really get what that is about. In general, however, php gives you a way of removing session data, and invalidating an id cookie. It is this: // 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 (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } // Finally, destroy the session. session_destroy(); Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 16, 2014 Author Share Posted August 16, 2014 Well I use a name space which will make my sessions look like $_SESSION['authentication']['id']; Would I put that instead of session_name()? Thank you Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 16, 2014 Author Share Posted August 16, 2014 using the code above will remove all sessions and data set...im only want to unset a particualr session not all...i tried using session_unset which worked great...although the function unappreciated Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted August 16, 2014 Share Posted August 16, 2014 First of all, this has absolutely nothing to do with “destroying a session”. All you want to do is unset a particular value of the $_SESSION array. The unset statement does exactly that. However, you can't just unset your own $sessions[$name] variable, because then you only delete this variable and not the original data. You need to actually unset $_SESSION[$namespace][$name]. Quote Link to comment Share on other sites More sharing options...
gizmola Posted August 18, 2014 Share Posted August 18, 2014 Your "namespacing" isn't anything more than setting up an array key under session. The code in question seems to be concerned with insuring that the actual session file is removed from the server. You aren't ever going to be able to do that with your "session namespace" because it depends on the continued existence of the session file. So -- to Jacques1 point, you should omit all the code that is attempting to mess around with the session lifetime settings (a really bad idea that would actually cause all the server session files to be removed) and just go with simple code that unsets the existing keys. 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.