php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7558 session - objects in arrays
Submitted: 2000-10-31 19:51 UTC Modified: 2000-11-27 08:44 UTC
From: info at borderblue dot com Assigned:
Status: Closed Package: Session related
PHP Version: 4.0.3pl1 OS: Red Hat Linux 6
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: info at borderblue dot com
New email:
PHP Version: OS:

 

 [2000-10-31 19:51 UTC] info at borderblue dot com
I'm using php.ini-optimized with register_globals = Off, so I access session variables using $HTTP_SESSION_VARS["blah"].  This is defined as an array of objects.  I'm able to access properties such as $HTTP_SESSION_VARS["blah"][0]->property, but I can't run methods by doing $HTTP_SESSION_VARS["blah"][0]->method().  The following test script illustrates the problem, reloading the page should increase the array size of "blah" each time.  I found that I could work around the problem by copying the session variable to a local variable, doing the methods on that, and then copying back.  I compiled PHP as a dynamic DSO module using:

./configure --with-mysql --with-apxs=/usr/local/apache/bin/apxs

Here is the test script:

/* reload the page to increase the array size of "blah" */
class Test {
   var $item;
   function run() {
      $this->item = 1234;
   }
}

session_start();
session_register("blah");
if (!isset($HTTP_SESSION_VARS["blah"])) {
   $HTTP_SESSION_VARS["blah"] = array();
}

echo "array size before = ",sizeof($HTTP_SESSION_VARS["blah"]),"<br>n";
$S = sizeof($HTTP_SESSION_VARS["blah"]);
$HTTP_SESSION_VARS["blah"][$S] = new Test;

/** this doesn't work *******************/
// $HTTP_SESSION_VARS["blah"][$S]->run();
/****************************************/

/** this works **************************/
$SESSION = $HTTP_SESSION_VARS;
$SESSION["blah"][$S]->run();
$HTTP_SESISON_VARS = $SESSION;
/****************************************/

echo "array size after = ",sizeof($HTTP_SESSION_VARS["blah"]),"<br>n";

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-01 23:28 UTC] info at borderblue dot com
I'm using php.ini-optimized with register_globals = Off, so I access session variables
using $HTTP_SESSION_VARS["blah"].  This is defined as an array of objects.  I'm able
to access properties such as $HTTP_SESSION_VARS["blah"][0]->property, but I
can't run methods by doing $HTTP_SESSION_VARS["blah"][0]->method().  The
following test script illustrates the problem, reloading the page should increase the array size
of "blah" each time.  I found that I could work around the problem by copying the
session variable to a local variable, doing the methods on that, and then copying back.  I
compiled PHP as a dynamic DSO module using:

./configure --with-mysql --with-apxs=/usr/local/apache/bin/apxs

Here is the test script:

/* reload the page to increase the array size of "blah" */
class Test {
   var $item;
   function run() {
      $this->item = 1234;
   }
}

session_start();
session_register("blah");
if (!isset($HTTP_SESSION_VARS["blah"])) {
   $HTTP_SESSION_VARS["blah"] = array();
}

echo "array size before =
",sizeof($HTTP_SESSION_VARS["blah"]),"<br>n";
$S = sizeof($HTTP_SESSION_VARS["blah"]);
$HTTP_SESSION_VARS["blah"][$S] = new Test;

/** this doesn't work *******************/
// $HTTP_SESSION_VARS["blah"][$S]->run();
/****************************************/

/** this works **************************/
$SESSION = $HTTP_SESSION_VARS;
$SESSION["blah"][$S]->run();
$HTTP_SESSION_VARS = $SESSION;
/****************************************/

echo "array size after =
",sizeof($HTTP_SESSION_VARS["blah"]),"<br>n";
 [2000-11-01 23:41 UTC] info at borderblue dot com
PLEASE NOTE:
------------
Ok, some comments on the above test code, it turns out I had a typo, so my "workaround" doesn't work.  Specifically, the line

$HTTP_SESSION_VARS = $SESSION;

was accidentally typed as

$HTTP_SESISON_VARS = $SESSION;

So the reason it worked is because the local $SESSION variable was being edited but it never got copied back to the session.  So restating the problems I'm seeing:

Objects inside arrays don't seem to work correctly in sessions.  The devshed.com site has some comments about this which say that maybe objects in arrays aren't properly deserialized in PHP 4.0.3 and 4.0.3pl1.  The relevant url is at

http://www.devshed.com/Server_Side/PHP/Commerce3/comments.html

It does not help to copy the entire session variable to a local variable.  However, I have some more test results for the test code shown above, in particular:

/** this does not work ******************/
$SESSION = $HTTP_SESSION_VARS["blah"];
$SESSION[$S]->run();
$HTTP_SESSION_VARS["blah"] = $SESSION;
/****************************************/

/** this works **************************/
$SESSION = $HTTP_SESSION_VARS["blah"][$S];
$SESSION->run();
$HTTP_SESSION_VARS["blah"][$S] = $SESSION;
/****************************************/

So if you copy the object itself, which is inside an array, to a local variable and execute the method there, you can copy back the result and the page seems to update properly when you reload.

 [2000-11-03 19:41 UTC] waldschrott@php.net
umh, in order to test this... well - PHP crashes on win32
and it does not work on Linux (did not check if segfaults)
 [2000-11-03 19:52 UTC] waldschrott@php.net
very strange, does not seem to have sth to do with
serialization, slightly modified script - the 4th line
crashes and the 5th does not, while both should be
completely equal (direct copy)

$SESSION = $HTTP_SESSION_VARS;
echo method_exists($SESSION["blah"][$S],'run');
echo method_exists($SESSION["blah"][$S],'run');
$SESSION["blah"][$S]->run();
// this line crashes
$HTTP_SESSION_VARS["blah"][$S]->run();
// this does not
$HTTP_SESSION_VARS = $SESSION;
 [2000-11-27 08:44 UTC] sniper@php.net
Should be fixed in CVS. Please try latest snapshot from
http://snaps.php.net/ and reopen this bug report if
this doesn't work with it.

--Jani
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 29 03:01:29 2024 UTC