php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47173 $_SESSION reference inside function broken
Submitted: 2009-01-20 19:27 UTC Modified: 2009-01-21 02:31 UTC
From: tommyhp2 at yahoo dot com Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.2.8 OS: Win03 R2 SP2 PHP ISAPI
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tommyhp2 at yahoo dot com
New email:
PHP Version: OS:

 

 [2009-01-20 19:27 UTC] tommyhp2 at yahoo dot com
Description:
------------
use of variable variables broken when reference inside a function in conjunction with $_SESSION.

Thanks,
Tommy

Reproduce code:
---------------
Scenario 1:
<code>
session_start();

$arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3');

if (isset($_SESSION['arr']))
{
	$arr =& $_SESSION['arr'];
} else {
	$_SESSION =& $arr;
}
</code>
Works as intended (please see "Expected result").

Scenario 2:
<code>
function register_session($var)
{
	global $$var;

	if (isset($_SESSION[$var]))
	{
		$$var =& $_SESSION[$var];
	} else
	{
		$_SESSION[$var] =& $$var;
	}
}

session_start();

$arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3');
register_session('arr');
</code>
Does not work as intended (Please see "Actual result").  The variable $arr is not referenced.

Expected result:
----------------
["_SESSION"]=>
  &array(3) {
    ["idx1"]=>
    string(4) "val1"
    ["idx2"]=>
    string(4) "val2"
    ["idx3"]=>
    string(4) "val3"
  }
  ["arr"]=>
  &array(3) {
    ["idx1"]=>
    string(4) "val1"
    ["idx2"]=>
    string(4) "val2"
    ["idx3"]=>
    string(4) "val3"
  }

Actual result:
--------------
["_SESSION"]=>
  &array(1) {
    ["arr"]=>
    array(3) {
      ["idx1"]=>
      string(4) "val1"
      ["idx2"]=>
      string(4) "val2"
      ["idx3"]=>
      string(4) "val3"
    }
  }
  ["arr"]=>
  array(3) {
    ["idx1"]=>
    string(4) "val1"
    ["idx2"]=>
    string(4) "val2"
    ["idx3"]=>
    string(4) "val3"
  }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-20 19:32 UTC] tommyhp2 at yahoo dot com
Same problem w/ CVS PHP 5.2 - Windows x86 VC6 (thread safe))  [10.02MB] - 2009-Jan-20 12:00:00
 [2009-01-21 00:12 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Do not do that..
 [2009-01-21 02:03 UTC] tommyhp2 at yahoo dot com
I don't understand how is this bogus? I did look at the manual to make sure I used the variable variables and reference correctly.  Looking at the code give, if I pass string 'arr' to the function, then $$var == ${'arr'}. There is no ambiguity.  If I do a var_dump($$var) equivalent to var_dump(${'arr'}) inside the function right after global $$var, I get the defined global $arr array contents.  I didn't see any where in the manual at
http://www.php.net/manual/en/language.variables.variable.php
about not being able to use it or implement it inside a function since the examples didn't implement inside a function.

As for references, I don't think it's used improperly. Since I could have multiple references to a single value:
$a = 'some string';
$b =& $a;
$c =& $a;

echo $b; // gives 'some string'
echo $c; // gives 'some string'

$a = 'nada';
echo $b; // gives 'nada'

which those 2 concepts, this code

$arr = array('idx1'=>'val1','idx2'=>'val2','idx3'=>'val3');
if (isset($_SESSION['arr']))
{
	$arr =& $_SESSION['arr'];
} else {
	$_SESSION =& $arr;
}

works as intended. If put the if/else inside a function and replace with the appropriate variable along with global definition, why does it break with no reference?  Or is referencing not permitted inside a function? Which I don't see in the manual. As for the example in the manual of 
function foo(&$var)
{
    $var++;
}
I'm not passing the value or the pointer/reference.  I'm passing a string and using the string via variable variables and global and referencing it as variable variables.

Thanks,
Tommy
 [2009-01-21 02:31 UTC] tommyhp2 at yahoo dot com
Sorry, the code in scenario 1 should be:

if (isset($_SESSION['arr']))
{
	$arr =& $_SESSION['arr'];
} else {
	$_SESSION['arr'] =& $arr;
}

Had a long day :D
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 18:01:32 2025 UTC