Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Session Handling Functions : session_unset

session_unset

Free all session variables (PHP 4, PHP 5)
void session_unset ( )

The session_unset() function frees all session variables currently registered.

Return Values

No value is returned.

Notes

Note:

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);.

Caution:

Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal.

Code Examples / Notes » session_unset

pentek_i

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.


07-mar-2006 03:56

Yes, that's how it is, first you write session_unset, and then the sentence session_distroy

21-mar-2001 04:58

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

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!


diogo dot afonso

The only thing needed to really destroy the session is :
session_unset();
session_destroy();


zach

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.


internic

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();


j a s o n p 0 1 9

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.


jeroen

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.

Change Language


Follow Navioo On Twitter
session_cache_expire
session_cache_limiter
session_commit
session_decode
session_destroy
session_encode
session_get_cookie_params
session_id
session_is_registered
session_module_name
session_name
session_regenerate_id
session_register
session_save_path
session_set_cookie_params
session_set_save_handler
session_start
session_unregister
session_unset
session_write_close
eXTReMe Tracker