note 71888 added to function.session-destroy
| From: | php-general at lists dot php dot net | Date: | Thu, 21 Dec 2006 08:59:31 +0000 |
| Subject: | note 71888 added to function.session-destroy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120819@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 83.137.20.120
Probable Submitter: 213.201.153.14 (proxied: 10.0.50.99)
----
Manual Page -- http://www.php.net/manual/en/function.session-destroy.php
Edit -- https://master.php.net/note/edit/71888
Del: integrated -- https://master.php.net/note/delete/71888/integrated
Del: useless -- https://master.php.net/note/delete/71888/useless
Del: bad code -- https://master.php.net/note/delete/71888/bad+code
Del: spam -- https://master.php.net/note/delete/71888/spam
Del: non-english -- https://master.php.net/note/delete/71888/non-english
Del: in docs -- https://master.php.net/note/delete/71888/in+docs
Del: other reasons-- https://master.php.net/note/delete/71888
Reject -- https://master.php.net/note/reject/71888
Search -- https://master.php.net/manage/user-notes.php