Hudzilla.org - the homepage of Paul Hudson
Contents > Cookies and Sessions > Using sessions Wish List | Report Bug | About Me ]

10.3.4     Removing session data

This is NOT the latest copy of this book; click here for the latest version.

Removing a specific value from a session is as simple as using the function unset(), just as you would for any other variable. It is important that you should unset only specific elements of the $_SESSION array, not the $_SESSION array itself, because that would leave you without any way to manipulate the session data at all.

To extend the previous script to remove data, use this:

<?php
    $_SESSION
['foo'] = 'bar';
    print
$_SESSION['foo'];
    unset(
$_SESSION['foo']);
?>




<< 10.3.3 Reading session data   10.3.5 Ending a session: session_destroy() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
!nxx - 07 Sep 2008

Thank you This helped me alot

thanks

A PHP User - 07 Sep 2008

I have a questions....
I understand that the sesion_start() function create a cookie variable which contain the session id. In the case that a user don't accept the cookies variables what's happen ?

alapidus1215@gmail.com - 07 Sep 2008

This is perfect if you only want to unset certain session variables. However, if you want to unset the whole session at once, it would be far easier and more efficient to use session_unset(), or session_unregister() for named sessions.

http://php.net/session-unset

A PHP User - 07 Sep 2008

This confused me for a while. Because I thought "what if the user does not go to a link which clears or unsets the session variable, what if they just close the browser?. What happens next? OH NO! The server is doomed with thousands of redundant session variables! ARRGHH!"...

Well I really think that this page should mention that session variables automatically gets cleared after a certain amount of time which can be specified in php.ini. Without it the shocking scenario above would have happened :)



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is two plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow