Zend Framework

After adding an array to a session namespace, adding elements to the array fails

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.7.0
  • Fix Version/s: 0.8.0
  • Component/s: Zend_Session
  • Labels:
    None

Description

-------- Original Message --------
Subject: [fw-auth] Problem with Zend Session and setting arrays
Date: Thu, 18 Jan 2007 07:00:40 -0800 (PST)
From: Daniel Schwip. (Germany) <ds@ms-computerservice.com>
To: fw-auth@lists.zend.com

Hi there,
if i set a session property to an array and later add a new key, it won't be
set. I think it's because of the __set php implementation???


$session = new Zend_session();
$session->array = array();
$session->array['testKey'] = 1;
Zend::Dump($session->array, 'sessArray'); // testKey is not set
// My Current workaround
$session->array = array_merge($session->array, array('testKey' => 1));

Issue Links

Activity

Hide
Gavin added a comment -

Oddly, I found setting additional keys in the array works, with only the first one "ignored" by PHP.
The problem certainly appears related to PHP and not Zend_Session.
Thus, a temporary workaround for the code above:

$session->array['dummy''] = true;
$session->array['testkey'] = 1;

$ php -v
PHP 5.1.6 (cgi-fcgi) (built: Oct 24 2006 19:38:28)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
$ svn update
uname At revision 3015.
$ uname -a
Linux sandbox.zend.com 2.6.9-34.EL #1 Fri Feb 24 16:44:51 EST 2006 i686 i686 i386 GNU/Linux

Show
Gavin added a comment - Oddly, I found setting additional keys in the array works, with only the first one "ignored" by PHP. The problem certainly appears related to PHP and not Zend_Session. Thus, a temporary workaround for the code above:
$session->array['dummy''] = true;
$session->array['testkey'] = 1;
$ php -v PHP 5.1.6 (cgi-fcgi) (built: Oct 24 2006 19:38:28) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies $ svn update uname At revision 3015. $ uname -a Linux sandbox.zend.com 2.6.9-34.EL #1 Fri Feb 24 16:44:51 EST 2006 i686 i686 i386 GNU/Linux
Hide
Gavin added a comment -

The result differs for PHP 5.2, and the "trick" above no longer works. All keys added are "ignored" by PHP.

$ php -v
PHP 5.2.0 (cgi-fcgi) (built: Dec 19 2006 15:37:32)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies

gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session
$ uname -a
CYGWIN_NT-5.1 IBM-F4F0A5528C1 1.5.22(0.156/4/2) 2006-11-13 17:01 i686 Cygwin

gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session
$ svn update
At revision 3016.

Show
Gavin added a comment - The result differs for PHP 5.2, and the "trick" above no longer works. All keys added are "ignored" by PHP. $ php -v PHP 5.2.0 (cgi-fcgi) (built: Dec 19 2006 15:37:32) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session $ uname -a CYGWIN_NT-5.1 IBM-F4F0A5528C1 1.5.22(0.156/4/2) 2006-11-13 17:01 i686 Cygwin gavin@IBM-F4F0A5528C1 ~/src/zftrunk/tests/Zend/Session $ svn update At revision 3016.
Hide
Gavin added a comment -

The following code produces inconsistent results on versions of PHP prior to PHP 5.2.1. Tests with 5.2.1RC4 seem to show the problem has been fixed with magic methods returning by reference.

<?php

session_start();

$_SESSION['anArray'] = array('a', 'b', 'c');
echo "\n\$_SESSION = ";
print_r($_SESSION);

class TestSessionRef
{
    public function &__get($name)
    {
        echo "\nReturning \$_SESSION[$name] by reference\n";
        return $_SESSION[$name];
    }
}

$test = new TestSessionRef();
$test->anArray['foo'] = 'bar';
echo "\n\$test->anArray = ";
print_r($test->anArray);

echo "\n\$a = ";
$a = & $test->anArray;
$a['bee'] = 'honey';
print_r($a);

echo "\n\$_SESSION = ";
print_r($_SESSION);

Work-around

Do not modify an array, after assigning it to a session namespace key.

Show
Gavin added a comment - The following code produces inconsistent results on versions of PHP prior to PHP 5.2.1. Tests with 5.2.1RC4 seem to show the problem has been fixed with magic methods returning by reference.
<?php

session_start();

$_SESSION['anArray'] = array('a', 'b', 'c');
echo "\n\$_SESSION = ";
print_r($_SESSION);

class TestSessionRef
{
    public function &__get($name)
    {
        echo "\nReturning \$_SESSION[$name] by reference\n";
        return $_SESSION[$name];
    }
}

$test = new TestSessionRef();
$test->anArray['foo'] = 'bar';
echo "\n\$test->anArray = ";
print_r($test->anArray);

echo "\n\$a = ";
$a = & $test->anArray;
$a['bee'] = 'honey';
print_r($a);

echo "\n\$_SESSION = ";
print_r($_SESSION);

Work-around

Do not modify an array, after assigning it to a session namespace key.
Hide
Gavin added a comment -

http://bugs.php.net/bug.php?id=36214

The new Zend_Session unit test passes with PHP 5.2.1RC4 (and hopefully will continue to pass with PHP 5.2.1+):
http://bugs.php.net/bug.php?id=39449

If no further problems are found, after PHP 5.2.1 is released, then I will close this issue.

Everyone using an earlier version of PHP should use the workaround above.

Show
Gavin added a comment - http://bugs.php.net/bug.php?id=36214 The new Zend_Session unit test passes with PHP 5.2.1RC4 (and hopefully will continue to pass with PHP 5.2.1+): http://bugs.php.net/bug.php?id=39449 If no further problems are found, after PHP 5.2.1 is released, then I will close this issue. Everyone using an earlier version of PHP should use the workaround above.
Hide
Gavin added a comment -

Another Work-Around

$myNamespace = new Zend_Session_Namespace('mySpace');

// works, even for broken versions of PHP
$a = array(1,2,3);
$myNamespace->someArray = array( & $a ) ;
$a['foo'] = 'bar';
Show
Gavin added a comment -

Another Work-Around

$myNamespace = new Zend_Session_Namespace('mySpace');

// works, even for broken versions of PHP
$a = array(1,2,3);
$myNamespace->someArray = array( & $a ) ;
$a['foo'] = 'bar';
Hide
Stephen St. Martin added a comment -

5.2.1 has been released, and i have confirmed the issue no longer exists, this could probably be closed.

Show
Stephen St. Martin added a comment - 5.2.1 has been released, and i have confirmed the issue no longer exists, this could probably be closed.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: