Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a PHP session?

Tags:

php

session

It's possible I'm not properly deleting PHP sessions when the user signs out. I've noticed that if I sign out and sign back in without closing the browser, the session ID doesn't change but if I sign out, close the browser window, open a new one and sign in, the session ID will be different. Do I need to be doing something different or is this normal behavior? I've been using the same process for three years but something happened recently that made me think that maybe I need to do something different.

Here's what I basically do when someone clicks Sign Out.

<?php

session_start();

if( isSet($_SESSION['FacID']) )
    $facID = $_SESSION['FacID'];    //Want to re-instate this after we destroy the session.

unset($_SESSION);
session_destroy();

if( isSet($_SESSION['FacID']) )
    $_SESSION['FacID'] = $facID;

?>
like image 267
user39653 Avatar asked Apr 17 '09 02:04

user39653


2 Answers

If you feel the need to force a new id http://pl.php.net/manual/en/function.session-regenerate-id.php

And to your question, from the manual:

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

like image 165
zalew Avatar answered Oct 06 '22 02:10

zalew


Your session is getting destroyed.

PHP will only generate a session id if the browser isn't specifying one. As long as the session has been destoryed, there is no problems with this.

like image 21
Nick Whaley Avatar answered Oct 06 '22 00:10

Nick Whaley