Re: session_destroy

From: Date: Wed, 19 Apr 2006 22:57:34 +0000
Subject: Re: session_destroy
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-234317@lists.php.net to get a copy of this message
Shannon Doyle wrote:
Hi People, Trying to get a session to destroy correctly, however the darn thing just refuses to destroy. I call the following in a separate webpage in a effort to destroy the session, only to find that the session still persists. <?php session_start(); session_unset(); session_destroy(); Header("Location: index.php"); ?> Any help would be appreciated.
I looked into really nuking a session quite a while back and after searching around and scraping together some snippets of code I came up with this: <?php function destroySession($delSessFile = false) {
    // 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()])) {
       $CookieInfo = session_get_cookie_params();
       if ( (empty($CookieInfo['domain'])) && (empty($CookieInfo['secure'])) ) {
           setcookie(session_name(), '', time()-3600, $CookieInfo['path']);
       } elseif (empty($CookieInfo['secure'])) {
           setcookie(session_name(), '', time()-3600, $CookieInfo['path'], $CookieInfo['domain']);
       } else {
           setcookie(session_name(), '', time()-3600, $CookieInfo['path'], $CookieInfo['domain'], $CookieInfo['secure']);
       }
    }
    // Finally, destroy the session.
    session_destroy();
    // go over board and actually delete the file right here and now!
    if ((boolean)$delSessFile) {
        $orgpath = getcwd(); /* chdir(PHP_BINDIR); */ chdir(session_save_path());
        $path = realpath(getcwd()).'/';
        if(file_exists($path.'sess_'.$id)) {
            // Delete it here
            unlink($path.'sess_'.$id);
        }
        chdir($orgpath);
    }
} ?> hth

« previous php.general (#234317) next »