0

我试图弄清楚为什么下面的代码不会跨页面保留我的 $_SESSION['objSession'] 对象,除非我将序列化/反序列化保留在下面。我厌倦了手动序列化/反序列化以在会话中进行对象更改,人们一直说我不应该这样做,但我确实看到其他关于会话对象在没有它的情况下不会在网络上持续存在的抱怨,包括堆栈上的...... PHP 5.3 阿帕奇 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 在此行下方...您可以忽略数据库包含,因为代码在不使用数据库功能的情况下存在相同的问题,并且无论如何我暂时对数据库功能进行了注释...。

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

?>
4

2 回答 2

0

这个问题似乎在你的clsSession课堂上。这是使用&. 由于会话对象已序列化,因此这些引用未正确存储。尝试删除这些(即更改UpdateSessionClassDataUpdateSession删除&from 参数),看看这是否解决了问题。

于 2013-01-18T00:51:51.757 回答
-1

开始,放在session_start();前面require_once并添加var_dump($_SESSION)调试。

于 2013-01-18T00:32:18.513 回答