PHP 4.0 Bug #7557 Updated: Array stored in session loose content

From: Date: Sat, 02 Dec 2000 18:23:23 +0000
Subject: PHP 4.0 Bug #7557 Updated: Array stored in session loose content
Groups: php.dev 
Request: Send a blank email to php-dev+get-39853@lists.php.net to get a copy of this message
ID: 7557 User Update by: twoerner@redhat.de Old-Status: Closed Status: Open Bug Type: *Session related Description: Array stored in session loose content Stage 1: ======= Ok, this version works with 4.0.4dev-200012020345. But it was an excerpt from my code. Stage 2: ======= Now i have extended the example and got the same error again :-( Please have a look at the code below: <? class C_CART_ITEM { var $pid; var $id=0; Function C_CART_ITEM() { } } class C_SESSION { var $cart1 = array(); var $cart2 = array(); Function C_SESSION() { } Function update() { $item = new C_CART_ITEM(); $item->pid = "".time(); array_push($this->cart1, $item); array_push($this->cart2, $item); } Function test() { for($i=0; $i<count($this->cart1); $i++) { $item = &$this->cart1[$i]; $item->pid .= "a"; } $item = $this->cart2; for($i=0; $i<count($item); $i++) { $item[$i]->pid .= "a"; } } Function show() { echo("ITEMS:<BR><TABLE CELLSPACING=2 CELLPADDING=0>"); $i = 0; while ($i < MAX(count($this->cart1),count($this->cart2))) { $item = &$this->cart1[$i]; $item->id += 1; echo("<TR><TD>".$item->pid." :".$item->id."</TD>"); $item = &$this->cart2; $item[$i]->id += 1; echo("<TD>".$item[$i]->pid." :".$item[$i]->id."</TD</TR>"); $i++; } echo("</TABLE><P>"); } } session_start(); if (!$session) { $session = new C_SESSION(); session_register("session"); } else { $session->test(); $session->update(); } echo "SESSION-ID: ".session_id()."<BR>"; $session->show(); ?> Have a look at the new test function. The line '$item = $this->cart2;' was first a mistake (it works on a copy and therefore doe not append the 'a' to the pid of other version). If this line is in there the same old problem is back again. The output looks like this: SESSION-ID: f0cf1206f566d170bf6fde86a5f76035 ITEMS: : 975780490 :7 : 975780490 :6 : 975780491 :5 : 975780491 :4 : 975780491 :3 : 975780491 :2 975780492 :1 975780492 :1 BUT: If you rename $item with $cart all is ok. If you replace '$item = $this->cart2;' with '$item = &$this->cart2;' all is ok. Stage 3: ======= I tried a third version with a inc and dec function in the C_CART_ITEM class wich increased and decreases $id and i called the dec and the inc function in C_SESSION->test and i got this error: Fatal error: Call to a member function on a non-object in ... Ouch! Previous Comments: --------------------------------------------------------------------------- [2000-11-27 08:52:26] 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 --------------------------------------------------------------------------- [2000-11-03 19:32:58] waldschrott@php.net works for me (4.0.4dev), please try 4.0.4 as far as it is out and report results --------------------------------------------------------------------------- [2000-11-02 03:09:55] twoerner@redhat.de Every call to the update function in the session class appends a new C_CART_ITEM with the actual time to the cart1 and cart2 arrays. If you reload the page several times, you get the following: SESSION-ID: 970efca9d3effdadf14e679d0e88b349 ITEMS: 973150040 : 1 973150036 : 8 : 1 973150038 : 7 : 1 973150039 : 6 : 1 973150039 : 5 : 1 973150039 : 4 : 1 973150040 : 3 : 1 973150040 : 2 : 1 973150040 : 1 The left two rows show the content of cart1 and the right 2 rows the content of cart2. Both arrays should have the same content. cart1 is referenced by item in the show function. And when this is done, this entry is not available in the next page (after reload etc.). cart2 is referenced as the whole array - this works. This bug occurs with PHP versions > 4.0.3 (4.0.2 not tested). It works fine with version 4.0.1pl1 . --------------------------------------------------------------------------- [2000-10-31 18:00:18] twoerner@redhat.de <? class C_CART_ITEM { var $pid; var $id=0; Function C_CART_ITEM() { } } class C_SESSION { var $cart1 = array(); var $cart2 = array(); Function C_SESSION() { } Function update() { $item = new C_CART_ITEM(); $item->pid = "".time(); array_push($this->cart1, $item); array_push($this->cart2, $item); } Function show() { echo("ITEMS:<BR><TABLE CELLSPACING=2 CELLPADDING=0>"); $i = 0; while ($i < MAX(count($this->cart1),count($this->cart2))) { $item = &$this->cart1[$i]; $item->id += 1; echo("<TR><TD>".$item->pid." : ".$item->id."</TD>"); $item = &$this->cart2; $item[$i]->id += 1; echo("<TD>".$item[$i]->pid." : ".$item[$i]->id."</TD</TR>"); $i++; } echo("</TABLE><P>"); } } session_start(); if (!$session) { $session = new C_SESSION(); session_register("session"); } else $session->update(); echo "SESSION-ID: ".session_id()."<BR>"; $session->show(); ?> --------------------------------------------------------------------------- Full Bug description available at: http://bugs.php.net/?id=7557

« previous php.dev (#39853) next »