note 62197 deleted from ref.session by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 18:21:06 +0000 |
| Subject: | note 62197 deleted from ref.session by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142218@lists.php.net to get a copy of this message | ||
Note Submitter: phpkloss at wholewheatradio dot org
----
If you're having trouble with unset($_SESSION[$something]) working, here's what I
discovered (Win2K/PHP 5.x.x). Assume you want to step through $_SESSION and delete (i.e. unset)
certain elements. So,
foreach ($_SESSION as $key=>$value) {
if (stristr($key,'something_to_delete')) {
echo "Will unset $key that has value of $value";
unset($_SESSION[$key]);
}
What I found was that although the $_SESSION elements were deleted in memory, the actual session
file was not being written out with the changes (even after using session_write_close). What fixed
it for me was to COPY $_SESSION INTO A DUMMY ARRAY, THEN STEP THROUGH THAT DUMMY ARRAY TO FIGURE OUT
WHICH ELEMENTS OF THE REAL $_SESSION TO DELETE. I.e.
foreach($_SESSION as $key=>$value) {
$dummy[$key]=$value; // copy to a dummy array
}
foreach ($dummy as $key=>$value) {
if (stristr($key,'something_to_delete')) {
echo "Will unset $key that has value of $value";
unset($_SESSION[$key]);
}
It appears that as you're stepping through the actual $_SESSION array in a foreach, if you
unset elements of it, those changes won't be recorded to the session disk file.
'Course then again, my coffee is running low so I could be wrong. But hopefully it's
something others can try who might be having a similar problem.