From:             
Operating system: Linux
PHP version:      5.2.13
Package:          Session related
Bug Type:         Bug
Bug description:$_SESSION nested array is lost when a foreach() dummy name 
matches array name

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.

-- 
Edit bug report at http://bugs.php.net/bug.php?id=51907&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=51907&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=51907&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=51907&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=51907&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=51907&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=51907&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=51907&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=51907&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=51907&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=51907&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=51907&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=51907&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=51907&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=51907&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=51907&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=51907&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=51907&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=51907&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=51907&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=51907&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=51907&r=mysqlcfg

Reply via email to