title: PHP Session Confusion
body: <p>Alright, I'm confused as hell. I have an object that I store in a session. I can add items to this object. Pretty simple so far. I initialize the object like this:</p>
  
  <pre><code>$template = new Template($mysqli);
  $_SESSION['template'] = serialize($template);
  </code></pre>
  
  <p>Now this should create a brand spanking new object and assign it to the session. I then have some code that adds items through an AJAX request. That code is as follows:</p>
  
  <pre><code>$template = unserialize($_SESSION['template']);
  $prodid = $_GET['product_id'];
  $template-&gt;addItem($prodid);
  echo var_dump($template-&gt;getItems());
  $_SESSION['template'] = serialize($template);
  </code></pre>
  
  <p>Again, should be simple. Now here's the issue, that first bit of code is not resetting <code>$_SESSION['template']</code> while it should, so I get all the items I've added so far, reloading the page doesn't fix it.</p>
  
  <p>I found the file that's causing the mischief but I don't know what I can do about this. It's an include and it's required for different portions of the site to function. I'm adding  functionality to the site, I don't think the owners would be please if I removed functionality. Here's the file:</p>
  
  <pre><code>&lt;?php
  
  include_once( 'DBE.class.php' ) ;
  
  ################################################
  # Function: Sessions_open
  # Parameters: $path (string), $name (string)
  # Returns: bool
  # Description: This is an over-ride function call
  #       that we need to create so that the php internal
  #       session manager doesn't store our session in the
  #       file system, since we are storing it in the
  #       db. Storing a session in a file system on the
  #       server inhibits scalability for two reasons:
  #       1: A large number of users may be hitting the site
  #           and clog the space on the hard-drive of the server
  #           due to the sheer amount of session files stored
  #       2: The website may be behind a load-balancer and
  #           therefore the server handling the page request
  #           may not have the session stored on its file system
  ################################################
  function Sessions_open ( $path, $name ) {
      return TRUE ;
  }
  
  
  ################################################
  # Function: Sessions_close
  # Parameters: N/A
  # Returns: bool
  # Description: This is an over-ride function call
  #       that we need to create so that the php internal
  #       session manager doesn't store our session in the
  #       file system, since we are storing it in the
  #       db. Storing a session in a file system on the
  #       server inhibits scalability for two reasons:
  #       1: A large number of users may be hitting the site
  #           and clog the space on the hard-drive of the server
  #           due to the sheer amount of session files stored
  #       2: The website may be behind a load-balancer and
  #           therefore the server handling the page request
  #           may not have the session stored on its file system
  ################################################
  function Sessions_close () {
      return TRUE ;
  }
  
  
  ################################################
  # Function: Sessions_read
  # Parameters: $SessionID (string)
  # Returns: (string) or (false) on error
  # Description: This function is used at startup to read
  #           the contents of the session. 
  #           If no sess data, the empty string ("") is returned.
  #           Otherwise, the serialized sess data is returned.
  #           On error, false is returned.
  ################################################
  function Sessions_read ( $SessionID ) {
  
      include_once( 'DBE.class.php' ) ;
      $dbe = new DBE() ;
  
      //default return value to false
      $returnVal = FALSE ;
  
      $query = "SELECT DataValue
                          FROM Sessions 
                          WHERE SessionID = '$SessionID' " ;
  
      $result = $dbe-&gt;Select( $query ) ;
  
      if( count( $result ) == 1 ) {
          $returnVal = $result[0]['DataValue'] ;
  
          //update the session so that we don't time-out after creating
          $query = "UPDATE Sessions
                              SET LastUpdated = NOW()
                              WHERE SessionID = '$SessionID'" ;
          $dbe-&gt;Update( $query ) ;
  
      } else {
          //Insert here to simplify the write function
          $query = "INSERT INTO Sessions (SessionID, DataValue) VALUES ( '$SessionID', '' )" ;
  
          $dbe-&gt;Insert( $query ) ;            //pass the insert stmt
  
          //set returnVal to '' being that we didn't find the SessionID
          $returnVal = '' ;
      }
  
      return( $returnVal ) ;
  }
  
  ################################################
  # Function: Sessions_write
  # Parameters: $SessionID (string), $Data
  # Returns: bool
  # Description: This function is used at startup to read
  #           the contents of the session. 
  #           If no sess data, the empty string ("") is returned.
  #           Otherwise, the serialized sess data is returned.
  #           On error, false is returned.
  ################################################
  function Sessions_write( $SessionID, $Data ) {
  
      include_once( 'DBE.class.php' ) ;
      $dbe = new DBE() ;
  
      //default to true
      $returnVal = TRUE ;
  
      //update the session
      $query = "UPDATE Sessions 
                              SET DataValue = '$Data'
                          WHERE SessionID = '$SessionID'" ;
  
      $result = $dbe-&gt;Update( $query ) ; //pass the update stmt to the dbEngine..
  
      //test for success
      if( $result == -1 )
          $returnVal = FALSE ;
  
      //return the return value
      return( $returnVal ) ;
  }
  
  
  ################################################
  # Function: Sessions_delete
  # Parameters: $SessionID (string)
  # Returns: bool
  # Description: This function is used to delete the session
  ################################################
  function Sessions_destroy( $SessionID ) {
  
      include_once( 'DBE.class.php' ) ;
      $dbe = new DBE() ;
  
      $query = "DELETE FROM Sessions WHERE SessionID = '$SessionID' " ;
  
      $dbe-&gt;Delete( $query ) ;
  
      return( TRUE ) ;
  }
  
  ################################################
  # Function: Sessions_delete
  # Parameters: $SessionID (string)
  # Returns: bool
  # Description: This function is used to delete the session
  ################################################
  function Sessions_gc( $aMaxLifetime ) {
  
      include_once( 'DBE.class.php' ) ;
      $dbe = new DBE() ;
  
      $query = "DELETE FROM Sessions WHERE (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP( LastUpdated )) &gt; $aMaxLifetime " ;
  
      $dbe-&gt;Delete( $query ) ;
  
      return( TRUE ) ;
  }
  
      session_set_save_handler( "Sessions_open", "Sessions_close",
                                   "Sessions_read", "Sessions_write",
                                   "Sessions_destroy", "Sessions_gc" ) ;
  
  ?&gt;
  </code></pre>
  
  <p>I think that this is changing the basic functionality of sessions, but I'm not quite sure. And that's causing my trouble with resetting the template in the session. Anyone have any ideas or know what I can do to fix this problem. I'm totally stumped so any help is greatly appreciated.</p>