php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42567 Iterate over $_SESSION won't allow to unset its fields
Submitted: 2007-09-05 15:39 UTC Modified: 2007-09-14 01:00 UTC
From: gustavoa at gmail dot com Assigned:
Status: No Feedback Package: Session related
PHP Version: 5.2.4 OS: Windows
Private report: No CVE-ID: None
 [2007-09-05 15:39 UTC] 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";

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-06 10:43 UTC] jani@php.net
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) {

 [2007-09-14 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC