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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gustavoa at gmail dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Mar 29 10:01:28 2024 UTC