Jump to content

sessions destroy


Destramic

Recommended Posts

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);
	}
Link to comment
Share on other sites

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();
Link to comment
Share on other sites

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].

Link to comment
Share on other sites

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.

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.