(PHP 4, PHP 5)
session_unset — Free all session variables
The session_unset() function frees all session variables currently registered.
没有返回值。
mwgamera at gmail dot com (2012-12-31 17:42:51)
There is nothing magical about the $_SESSION superglobal and unsetting it.
One has simply to remember that all the session_* functions will use continue to
use exactly that instance of array that was set up by session_start.
Consider this example:
<?php
session_start();
var_dump($_SESSION);
$a =& $_SESSION;
unset($_SESSION);
$a['x'] = 1; // $a refers to the original $_SESSION
$_SESSION['x'] = 2; // new unrelated array
session_write_close();
// saved session contains x => 1
?>
espertalhao04 at hotmail dot com (2012-07-17 02:06:54)
i always do like this to logout:
<?php
session_start();
session_destroy();
?>
worked 100% with me, on ie, opera (windows, puppy linux 4.3.1, ubuntu and android), firefox, chrome, dolphin hd (android) and even android 2.1 default web browser!
you can try to check the content and destroy it like this:
<?php
session_start();
session_destroy();
if(count($_SESSION) == 0)
{
$_SESSION=array();
session_destroy();
}
?>
remember:
allways put this in the very beginning of the html code or other php scripts...
if you put under <html> tag, you gonna have a bad time...
tim at leethost dot com (2012-01-05 15:50:39)
I was having a problem clearing all session variables, deleting the session, and creating a new session without leaving old session stuff behind in all browsers. The below code is perfect for a logout script to totally delete everything and start new. It even works in Chrome which seems to not work as other browsers when trying do logout and start a new session.
<?php
session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
?>
notsonewbie (2011-01-05 15:56:28)
dash2nash: you have to start the session before you can unset or destroy it.
dash2nash (2010-10-21 06:16:53)
Im using chrome and Im not a pro in php, but Im certainly sure that session sure is buggy on google chrome, I use:
session_unset();
session_destroy();
$_SESSION = array();
session_start();
every time the page load, yet, some of the session still there. It's quite scary problem :s
angelitoestebancito at gmail dot com (2008-08-16 14:41:25)
If you create a logout.php to end user session, do not forget to start the session in this page!, using session_start() at the very begging of your script. Thus,
session_start();
session_unset();
session_destroy();
will be the right sequence to end a user's session.
This is a very basic concept, but took me a while to realize I was forgetting to start the session.
00 at f00n dot com (2008-06-23 19:00:27)
-------------------------------------------------------
InterNic
20-May-2006 11:20
sometimes you might have problems even if using both session_unset and session_destroy. You have to clear the $_SESSION array. I got it working this way:
session_unset();
session_destroy();
$_SESSION = array();
-------------------------------------------------------
When I used this method the session still existed in structure until the array is reset completely.
This, however, has the drawback of clearing all sessions which may not be ideal in some cases.
Named sessions may help in such cases.
pentek_i at inf dot elte dot hu (2006-08-08 16:54:22)
You should know that on recent PHP only the first one of these functions works correctly. And if you use the other two, var_dump will print you the result you expected (session cleaned up), but the session file on the server won't be cleaned up. So use the first one.
<?php
function session_clean1($logout=false)
{
$v=array();
foreach($_SESSION as $x=>$y)
if($x!="redirector"&&($x!="user"||$logout))
$v[]=$x;
foreach($v as $x)
unset($_SESSION[$x]);
return;
}
function session_clean2($logout=false)
{
foreach($_SESSION as $x=>$y)
if($x!="redirector"&&($x!="user"||$logout))
unset($_SESSION[$x]);
return;
}
function session_clean3($logout=false)
{
$s=($logout||!isset($_SESSION["user"]))?array():
array("user"=>$_SESSION["user"]);
if(isset($_SESSION["redirector"]))
$s["redirector"]=$_SESSION["redirector"];
$_SESSION=$s;
}
?>
On previous php (<<5.1.4) releases at least the third one worked correctly.
InterNic (2006-05-20 03:20:52)
sometimes you might have problems even if using both session_unset and session_destroy. You have to clear the $_SESSION array. I got it working this way:
session_unset();
session_destroy();
$_SESSION = array();
zach at zkwarta dot com (2005-07-13 15:32:03)
The difference between both session_unset and session_destroy is as follows:
session_unset just clears out the sesison for usage. The session is still on the users computer. Note that by using session_unset, the variable still exists.
Using session_unset in tandem with session_destroy however, is a much more effective means of actually clearing out data. As stated in the example above, this works very well, cross browser:
session_unset();
session_destroy();
I noticed that in firefox, one could simply use sesison_unset and the session would be cleared. When trying this on IE, I was horrified to find out that the data was still there, so I had to use session destroy.
Jeroen (2005-01-15 01:42:57)
note to Jason: I don't know the exact mechanics of it (since I'm quite new to sessions) but I think you need to use session_unset() BEFORE you can use session_destroy() at all. I thought that session_unset() was for scripted variables, and session_destroy() just for anything saved on your side regarding the session.
(2001-03-21 08:58:39)
To further clarify the note above... this can be done via the session handling directives in your php.ini file... there are options to set garbage collection probability (via percent... i.e. 75 means it would run 3 out of every 4 page accesses), and the amount of time a session object can remain active before the garbage collection process sees it as garbage.
dmertens at zyprexia dot com (2001-01-31 09:11:09)
The session files are automaticly deleted after the session-timeout is reached. So if the time-out is set to 20 minutes, the files will be deleted 20 minutes after the last access. Same for the cookie. Every time, an page is requested, the cookie-ttl is set to now + 20 minutes.
PHP is a very clean scripting engine, which leaves no garbage on your system!
j a s o n p 0 1 9 at yahoo dot com (2000-07-13 13:26:13)
session_unset() vs. session_destroy():
I would say that the difference is that session_destroy() destroys the session variables, both in the script and where the session data is stored on disk. session_unset() is like doing a session_unregister() on all registered variables. They can still be re-registered by calling session_register() whereas after session_destroy, they cannot.