note 62197 added to ref.session

From: Date: Wed, 22 Feb 2006 08:57:28 +0000
Subject: note 62197 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-104202@lists.php.net to get a copy of this message
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. ---- Server IP: 64.71.164.2 Probable Submitter: 216.152.181.196 ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62197 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62197&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62197&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104202) next »