php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51907 $_SESSION nested array is lost when a foreach() dummy name matches array name
Submitted: 2010-05-25 19:54 UTC Modified: 2010-05-26 10:03 UTC
From: danieln at javanet dot com Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: danieln at javanet dot com
New email:
PHP Version: OS:

 

 [2010-05-25 19:54 UTC] danieln at javanet dot com
Description:
------------
I'd appreciate confirmation that the following is a bug with PHP 5.2.13, and apologies if I'm mistaken.

The following complete PHP script crashes the 3rd time it is run.  The foreach() loop seems to have an (improper) side effect which wipes out the nested $_SESSION array because the internal variable used in the loop matches the name of the nested $_SESSION array.  Simply changing the name of the internal foreach() variable to something different fixes the problem.

Note: clear session variables before running script 3 times.

Again, changing "$breadcrumb" to "$the_breadcrumb" fixes the problem.  But the foreach() loop should have no side effects.  Note: since the scope of $breadcrumb is not the same as the scope of $_SESSION["breadcrumb"], there should be no collision.

Note that doing a print_r() on the array shows the array as (correctly) empty the first time, (correctly) populated the second time, and erroneously set as "Array ( [breadcrumb] => trail )" the third time (the nested array has been wiped out). 

The error in the PHP error log from the 3rd run:
PHP Fatal error:  Cannot use string offset as an array on line 5

The issue is not a problem on PHP 5.3 - only PHP 5.2.13.  I could not find any note regarding this issue in the PHP changelog, and I must use 5.2.13 on my live site, so I'm posting here for confirmation that this is a PHP 5.2 bug.

Thanks,
Dan Nissenbaum

Test script:
---------------
<?php
	session_start();
	if (!is_array($_SESSION["breadcrumb"]["trail"]))
	{
		$_SESSION["breadcrumb"]["trail"][] = "trail";
	}
	foreach ($_SESSION["breadcrumb"]["trail"] as $breadcrumb)
	{
		echo $breadcrumb;
	}
?>


Expected result:
----------------
No PHP 5.2.13 crash on line 5.

Actual result:
--------------
PHP 5.2.13 crashes on line 5.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-25 22:25 UTC] danieln at javanet dot com
-Status: Open +Status: Closed
 [2010-05-25 22:25 UTC] danieln at javanet dot com
Solved - Not a bug.  Sorry for false alarm.  I was not aware of the "register_global" php.ini setting, which merges the scope of local and superglobal arrays.
 [2010-05-26 10:03 UTC] mike@php.net
-Status: Closed +Status: Bogus
 [2010-05-26 10:03 UTC] mike@php.net
Not a bug -> Bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 01:01:30 2024 UTC