php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7557 Array stored in session loose content
Submitted: 2000-10-31 18:00 UTC Modified: 2001-02-19 22:30 UTC
From: twoerner at redhat dot de Assigned:
Status: Closed Package: Session related
PHP Version: 4.0.3pl1, 4.0.4 OS: RedHat 7
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: twoerner at redhat dot de
New email:
PHP Version: OS:

 

 [2000-10-31 18:00 UTC] twoerner at redhat dot 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();
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-02 03:09 UTC] twoerner at redhat dot 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-11-03 19:32 UTC] 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-27 08:52 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
 [2000-12-02 13:23 UTC] twoerner at redhat dot de
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!

 [2001-02-19 22:30 UTC] andre@php.net
this is rather a confusion how references work than a bug
if you had var_dump()ed your $session you would have seen
that session handling cannot be involved here

what you are doing is
a) creating a reference $item to an $item in cart1
b) and then storing the whole cart2 therein 

chaning (b) to $item = &$this->cart2; works for me


    Function test() {
      for($i=0; $i<count($this->cart1); $i++) {
        $item = &$this->cart1[$i]; // (a)
        $item->pid .= "a";
      }

      $item = $this->cart2; // (b)
      for($i=0; $i<count($item); $i++) {
        $item[$i]->pid .= "a";
      }
    }

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 10:01:31 2024 UTC