note 71888 deleted from function.session-destroy by colder

From: Date: Thu, 21 Dec 2006 16:07:21 +0000
Subject: note 71888 deleted from function.session-destroy by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-120834@lists.php.net to get a copy of this message
Note Submitter: ---- Bothering with the timestamp can always give troubles. A lot better is forcing the sessionid to be regenrerated. A trick to destroy the session completly is actually restarting the session, like someone closed and reopened his browser. This will fix the whole authority problem and browser cookie deletion problem quite more easy, and it gives cleaner code without having to clean the $_SESSION array. I would suggest the following function session_restart(); <?php session_start(); // Some simple code etc etc $requested_logout = true; if ($requested_logout) { session_restart(); } // Now the session_id will be different every browser refresh print(session_id()); function session_restart() { if (session_name()=='') { // Session not started yet session_start(); } else { // Session was started, so destroy session_destroy(); // But we do want a session started for the next request session_start(); session_regenerate_id(); // PHP < 4.3.3, since it does not put setcookie(session_name(), session_id()); } } ?> NOTE: session_restart() acts like session_start(), so no output must be written before its called.

« previous php.notes (#120834) next »