PHP OOP会话对象-赢得';在不序列化的情况下无法持久化取消序列化

PHP OOP会话对象-赢得';在不序列化的情况下无法持久化取消序列化,php,Php,我试图弄明白为什么下面的代码不会跨页面持久化我的$_SESSION['objSession']对象,除非我在下面保持序列化/取消序列化。我厌倦了手动序列化/取消序列化以在会话中进行对象更改,人们一直说我不应该这样做,但我确实看到了其他关于会话对象在web上(包括堆栈上)没有它就无法持久的抱怨。。。PHP5.3Apache2.2Windows2008 <?php require_once("/php/php_clsSession.php");?> <?php session_st

我试图弄明白为什么下面的代码不会跨页面持久化我的$_SESSION['objSession']对象,除非我在下面保持序列化/取消序列化。我厌倦了手动序列化/取消序列化以在会话中进行对象更改,人们一直说我不应该这样做,但我确实看到了其他关于会话对象在web上(包括堆栈上)没有它就无法持久的抱怨。。。PHP5.3Apache2.2Windows2008

<?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);
?>

----此行下面的会话。。。您可以忽略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();
            }
        }
    }

?>


要启动,请将
会话_start()require\u once
之前添加
var\u dump($\u SESSION)
进行调试。

问题似乎出现在
clsSession
类中。这是使用
&
。由于会话对象已序列化,因此这些引用未正确存储。尝试删除这些(即,更改
UpdateSessionClassData
UpdateSession
以从参数中删除
&
),并查看这是否会对问题进行排序。

他们删除了另一侧的伪行。。。错别太多了,别认为这是问题所在。你看到了什么问题?当您选中?thx Jim-isset tests对辅助页面请求为true时,更改不会出现或是
$\u SESSION['objSession']
null,但最初使用CreateSession方法设置的属性值为null。我可以在同一代码旁边创建另一个简单的$\u会话变量,该变量仅为文本/数字,并且无论何时都将保持不变。请也发布您的对象定义。如果将
SESSION\u write\u close()
放在文件结尾之前(而不使用un-/serialize),会发生什么情况
在请求之前不是一个好主意。PHP序列化进入会话的对象,并且在取消序列化时类定义必须存在。确切地说,我最初遇到了会话_start()的第一个问题,也是同样的问题,但我将它移到了第二行,因为会话对象必须先在那里。无论会话本身是否持久化objSession对象,问题在于对象中设置的属性没有持久化。简单的会话变量在同一代码中工作正常,但问题只发生在分配对象时。。。我在数百次迭代中完美地摆脱了引用传递非常感谢你。。。。顺便说一句,如果您对我如何不必进行tmpobj属性更改,然后每次更改时必须将整个对象重新分配回会话['objSession']有任何想法,请告诉我。我希望拥有intellisense并能够直接更改对象属性。@TimWiley如果我记得在会话中正确地更新对象,则无需重新插入会话即可工作。