PHP OOP会话对象 - 如果没有Seri​​alize Un-Serialize将不会持久化

时间:2013-01-18 00:15:12

标签: php

我正在试图找出为什么下面的代码不会在页面中保留我的$ _SESSION ['objSession']对象,除非我保持序列化/反序列化在下面。我厌倦了手动序列化/反序列化以在会话中进行对象更改,人们一直说我不应该这样做,但我确实看到其他关于会话对象的抱怨,如果没有它在网上就不会持续存在,包括这里的堆栈...... PHP 5.3 Apache 2.2 Windows 2008.

<?php require_once("/php/php_clsSession.php");?>
<?php session_start(); ?>
<?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->CreateSession(session_id(),$_SERVER);
    }
    else {
        // this code will only run if the session is already 
        // set
        $objSession = unserialize($_SESSION['objSession']);
        $objSession->UpdateSession(session_id(),$_SERVER);
    }
    // Update Session Object
    $_SESSION['objSession'] = serialize($objSession);
    unset($objSession);
?>

---- clsSession在这一行之下......你可以忽略db include,因为代码有相同的问题而不使用db功能,我无论如何都会暂时评论db函数....

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

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

      // Writes the session data to database
      public function WriteSessionToDb($xUserType = "Web") {
              $objConnect = new clsDb;
              $objDb = $objConnect->GetDbConnection($xUserType);
              //$objDb = $this->GetDbConnection($xUserType);
              $_InsertSQL = new PDOStatement;
              $_InsertSQL = $objDb->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->bindParam(1, $this->Action_Dt );
              $_InsertSQL->bindParam(2, $this->Session_Id );
              $_InsertSQL->bindParam(3, $this->Session_IP_Address );
              $_InsertSQL->bindParam(4, $this->Browser_Type );
              $_InsertSQL->bindParam(5, $this->Display_Resolution );
              $_InsertSQL->bindParam(6, $this->Page_Requested );
              $_InsertSQL->bindParam(7, $this->Page_Request_Params );
              $_InsertSQL->bindParam(8, $this->Request_Method );
              $_InsertSQL->bindParam(9, $this->Is_Https_Ind );
              $_InsertSQL->bindParam(10, $this->Is_Logged_In_Ind );
              $_InsertSQL->bindParam(11, $this->User_Key );
          try {
            $objDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $objDb->beginTransaction();
            $_InsertSQL->execute();
            $objDb->commit();
            unset($objDb);
          } catch (Exception $e) {
            $objDb->rollBack();
            echo "Failed: " . $e->getMessage();
            unset($objDb);
            unset($objConnect);
          }
      }

        // Writes the session data to database
        public function WriteSessionActivityToDb($xUserType = "Web",$xPageAction = "N/A") {
                $objConnect = new clsDb;
                $objDb = $objConnect->GetDbConnection($xUserType);
                //$objDb = $this->GetDbConnection($xUserType);
                $_InsertSQL = new PDOStatement;
                $_InsertSQL = $objDb->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->bindParam(1, $this->Action_Dt );
                $_InsertSQL->bindParam(2, $this->Session_Id );
                $_InsertSQL->bindParam(3, $this->Session_IP_Address );
                $_InsertSQL->bindParam(4, $this->Page_Requested );
                $_InsertSQL->bindParam(5, $this->Page_Request_Params );
                $_InsertSQL->bindParam(6, $this->Request_Method );
                $_InsertSQL->bindParam(7, substr(trim($xPageAction),0,32));
                $_InsertSQL->bindParam(8, $this->Is_Https_Ind );
                $_InsertSQL->bindParam(9, $this->Is_Logged_In_Ind );
                $_InsertSQL->bindParam(10, $this->User_Key );
            try {
              $objDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $objDb->beginTransaction();
              $_InsertSQL->execute();
              $objDb->commit();
              unset($objDb);
              unset($objConnect);
            } catch (Exception $e) {
              $objDb->rollBack();
              unset($objDb);
              echo "Failed: " . $e->getMessage();
            }
        }
    }

?>

2 个答案:

答案 0 :(得分:0)

问题似乎出现在clsSession课程中。这是使用&。由于会话对象是序列化的,因此这些引用未正确存储。尝试删除这些内容(例如,更改UpdateSessionClassDataUpdateSession以从参数中删除&),看看是否会对问题进行排序。

答案 1 :(得分:-1)

开始,将session_start();放在require_once之前,然后添加var_dump($_SESSION)进行调试。