PHP OOP Session Object - Won't Persist Without Serialize Un-Serialize
<p>I'm trying to figure out why the code below won't persist my $_SESSION['objSession'] object across pages unless i keep the serialize/unserialize in place below.  I get tired of manually serializing/unserializing to make object changes in the session and people keep saying i shouldn't have to do it but i do see other complaints about session objects not persisting without it on the web including here on stack... PHP 5.3 Apache 2.2 Windows 2008.   </p>

<pre><code>&lt;?php require_once("/php/php_clsSession.php");?&gt;
&lt;?php session_start(); ?&gt;
&lt;?php
    // Session Object Create/Log
    $objSession = new clsSession;
    if ( !(isset($_SESSION['objSession']) )) {
        // This line will populate some properties in the obj
        // like Session_ID and Create_dt
        $objSession-&gt;CreateSession(session_id(),$_SERVER);
    }
    else {
        // this code will only run if the session is already 
        // set
        $objSession = unserialize($_SESSION['objSession']);
        $objSession-&gt;UpdateSession(session_id(),$_SERVER);
    }
    // Update Session Object
    $_SESSION['objSession'] = serialize($objSession);
    unset($objSession);
?&gt;
</code></pre>

<p>---- clsSession Below this line... you can ignore the db include as the code has the same problem without using the db functionality and i have the db function temporarily commented anyhow.... </p>

<pre><code>&lt;?php
   // -----------------------------------------------------------------
   // Program Type: Class
   // Program Name: clsSession
   // Program Date: 01/08/2012 Programmer: Tim Wiley
   // Description:  Standard class for session creation/update
   // -----------------------------------------------------------------
  class clsSession {

       // Properties
       public $Session_Id = null;
       public $Creation_Dt = null;
       public $Action_Dt = null;
       public $Session_IP_Address = null;
       public $Browser_Type = null;
       public $Display_Resolution = null;
       public $Is_Https_Ind = null;
       public $Is_Logged_In_Ind = 0;
       public $User_Key = null;
       public $User_Id = null;
       public $Email_Address = null;
       public $Request_Method = null;
       public $Page_Requested = null;
       public $Page_Request_Params = null;
       public $Page_Action = null;
       public $Login_Attempts = 0;
       public $Max_Login_Attempts = 3;

     private function UpdateSessionClassData (&amp;$xSessionId = null, &amp;$xSessionObj = null, &amp;$xPageAction = "N/A" ) {
        $this-&gt;Session_Id = &amp;$xSessionId;
        $this-&gt;Action_Dt = date( 'Y-m-d H:i:s', time( ));
        $this-&gt;Session_IP_Address = substr(trim(&amp;$xSessionObj['REMOTE_ADDR']),0,24);
        $this-&gt;Browser_Type = substr(trim(&amp;$xSessionObj['HTTP_USER_AGENT']),0,140);
        $this-&gt;Request_Method = substr(trim(&amp;$xSessionObj['REQUEST_METHOD']),0,24);
        $this-&gt;Page_Requested = substr(trim(&amp;$xSessionObj['SCRIPT_NAME']),0,140);
        $this-&gt;Page_Request_Params = substr(trim(&amp;$xSessionObj['QUERY_STRING']),0,140);
        $this-&gt;Is_Https_Ind = &amp;$xSessionObj['SERVER_PORT'] == 443 ? 1 : 0;
        if (is_null($this-&gt;Display_Resolution)) {
            require_once('/javascript/js_SaveScreenResolutionInCookie.js');
            $this-&gt;Display_Resolution = !( IS_NULL( $_COOKIE['users_resolution'] )) ? substr(trim($_COOKIE['users_resolution']),0,16) : "N/A";
        }
        $this-&gt;Page_Action = substr(trim(&amp;$xPageAction),0,32);
     }
     // Initialize Session objSession for $_SESSION
     public function CreateSession($xSessionId = null, &amp;$xSessionObj = null ) {
        $this-&gt;Creation_Dt = date( 'Y-m-d H:i:s', time( ));
        $this-&gt;UpdateSessionClassData(&amp;$xSessionId, &amp;$xSessionObj);
       // $this-&gt;WriteSessionToDb();
        }

     // Update Session objSession for $_SESSION
     public function UpdateSession($xSessionId = null, &amp;$xSessionObj = null, $xPageAction = "N/A" ) {
        $this-&gt;UpdateSessionClassData(&amp;$xSessionId, &amp;$xSessionObj, &amp;$xPageAction);
       // $this-&gt;WriteSessionActivityToDb();
        }

      // Writes the session data to database
      public function WriteSessionToDb($xUserType = "Web") {
              $objConnect = new clsDb;
              $objDb = $objConnect-&gt;GetDbConnection($xUserType);
              //$objDb = $this-&gt;GetDbConnection($xUserType);
              $_InsertSQL = new PDOStatement;
              $_InsertSQL = $objDb-&gt;prepare("INSERT INTO T_SESSION_STATS(" .
                  "F_ACTION_DT, F_SESSION_ID, F_SESSION_IP_ADDRESS, F_BROWSER_TYPE," .
                  "F_DISPLAY_RESOLUTION, F_PAGE_REQUESTED, F_PAGE_REQUEST_PARAMS," .
                  "F_REQUEST_METHOD, F_IS_HTTPS_IND, F_IS_LOGGED_IN_IND, F_USER_KEY)" .
                  "Values (?,?,?,?,?,?,?,?,?,?,?)");
              $_InsertSQL-&gt;bindParam(1, $this-&gt;Action_Dt );
              $_InsertSQL-&gt;bindParam(2, $this-&gt;Session_Id );
              $_InsertSQL-&gt;bindParam(3, $this-&gt;Session_IP_Address );
              $_InsertSQL-&gt;bindParam(4, $this-&gt;Browser_Type );
              $_InsertSQL-&gt;bindParam(5, $this-&gt;Display_Resolution );
              $_InsertSQL-&gt;bindParam(6, $this-&gt;Page_Requested );
              $_InsertSQL-&gt;bindParam(7, $this-&gt;Page_Request_Params );
              $_InsertSQL-&gt;bindParam(8, $this-&gt;Request_Method );
              $_InsertSQL-&gt;bindParam(9, $this-&gt;Is_Https_Ind );
              $_InsertSQL-&gt;bindParam(10, $this-&gt;Is_Logged_In_Ind );
              $_InsertSQL-&gt;bindParam(11, $this-&gt;User_Key );
          try {
            $objDb-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $objDb-&gt;beginTransaction();
            $_InsertSQL-&gt;execute();
            $objDb-&gt;commit();
            unset($objDb);
          } catch (Exception $e) {
            $objDb-&gt;rollBack();
            echo "Failed: " . $e-&gt;getMessage();
            unset($objDb);
            unset($objConnect);
          }
      }

        // Writes the session data to database
        public function WriteSessionActivityToDb($xUserType = "Web",$xPageAction = "N/A") {
                $objConnect = new clsDb;
                $objDb = $objConnect-&gt;GetDbConnection($xUserType);
                //$objDb = $this-&gt;GetDbConnection($xUserType);
                $_InsertSQL = new PDOStatement;
                $_InsertSQL = $objDb-&gt;prepare("INSERT INTO T_SESSION_ACTIVITIES(" .
                    "F_ACTION_DT, F_SESSION_ID, F_SESSION_IP_ADDRESS, " .
                    "F_PAGE_REQUESTED, F_PAGE_REQUEST_PARAMS," .
                    "F_REQUEST_METHOD, F_PAGE_ACTION, F_IS_HTTPS_IND, F_IS_LOGGED_IN_IND, F_USER_KEY)" .
                    "Values (?,?,?,?,?,?,?,?,?,?)");
                $_InsertSQL-&gt;bindParam(1, $this-&gt;Action_Dt );
                $_InsertSQL-&gt;bindParam(2, $this-&gt;Session_Id );
                $_InsertSQL-&gt;bindParam(3, $this-&gt;Session_IP_Address );
                $_InsertSQL-&gt;bindParam(4, $this-&gt;Page_Requested );
                $_InsertSQL-&gt;bindParam(5, $this-&gt;Page_Request_Params );
                $_InsertSQL-&gt;bindParam(6, $this-&gt;Request_Method );
                $_InsertSQL-&gt;bindParam(7, substr(trim($xPageAction),0,32));
                $_InsertSQL-&gt;bindParam(8, $this-&gt;Is_Https_Ind );
                $_InsertSQL-&gt;bindParam(9, $this-&gt;Is_Logged_In_Ind );
                $_InsertSQL-&gt;bindParam(10, $this-&gt;User_Key );
            try {
              $objDb-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $objDb-&gt;beginTransaction();
              $_InsertSQL-&gt;execute();
              $objDb-&gt;commit();
              unset($objDb);
              unset($objConnect);
            } catch (Exception $e) {
              $objDb-&gt;rollBack();
              unset($objDb);
              echo "Failed: " . $e-&gt;getMessage();
            }
        }
    }

?&gt;
</code></pre>
