PHP 4.0 Bug #7558 Updated: session - objects in arrays

From: Date: Mon, 27 Nov 2000 13:44:51 +0000
Subject: PHP 4.0 Bug #7558 Updated: session - objects in arrays
Groups: php.dev 
Request: Send a blank email to php-dev+get-39253@lists.php.net to get a copy of this message
ID: 7558 Updated by: sniper Reported By: info@borderblue.com Status: Closed Bug Type: *Session related Assigned To: Comments: 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 Previous Comments: --------------------------------------------------------------------------- [2000-11-03 19:52:51] 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-03 19:41:22] 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-01 23:41:25] info@borderblue.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-01 23:28:07] info@borderblue.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-10-31 19:51:37] info@borderblue.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"; --------------------------------------------------------------------------- The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online. Full Bug description available at: http://bugs.php.net/?id=7558

« previous php.dev (#39253) next »