#42567 [Opn->Fbk]: Iterate over $_SESSION won't allow to unset its fields

From: Date: Thu, 06 Sep 2007 10:43:08 +0000
Subject: #42567 [Opn->Fbk]: Iterate over $_SESSION won't allow to unset its fields
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-117213@lists.php.net to get a copy of this message
ID: 42567 Updated by: jani@php.net Reported By: gustavoa at gmail dot com -Status: Open +Status: Feedback Bug Type: Session related Operating System: Windows PHP Version: 5.2.4 New Comment: Please read: http://php.net/foreach There you find this: "As of PHP 5, you can easily modify array's elements by preceding $value with &." So this should work: foreach ($_SESSION as $key => &$value) { Previous Comments: ------------------------------------------------------------------------ [2007-09-05 15:39:53] gustavoa at gmail dot com Description: ------------ The following session is stored in my temp directory: old_var2|s:4:"foo2"; I try to iterate over the $_SESSION var (using foreach) to unset some session keys, in particular those who start with "old_" (see the attached code). But I can't. Besides, if after this iteration I try to modify the $_SESSION var nothing happens. I'm using IIS 5.1 (blame my boss). Reproduce code: --------------- session_start(); $prefix = 'old_'; foreach ($_SESSION as $key => $value) { if (substr($key, 0, 4) == $prefix) { unset($_SESSION[$key]); } } $_SESSION['var1'] = 'foo'; Expected result: ---------------- This change in the session file: var1|s:3:"foo"; Actual result: -------------- old_var2|s:4:"foo2"; ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42567&edit=1

« previous php.bugs (#117213) next »