0

以下のコードがシリアライズ/アンシリアライズを維持しない限り、ページ全体で $_SESSION['objSession'] オブジェクトを永続化しない理由を理解しようとしています。セッションでオブジェクトを変更するために手動でシリアル化/非シリアル化することにうんざりしていて、人々はそれを行う必要はないと言い続けていますが、ここを含むWeb上でそれなしではセッションオブジェクトが持続しないという他の苦情を見ていますスタック... 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 この行の下...コードにはdb機能を使用せずに同じ問題があり、とにかく一時的にコメントされた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();
            }
        }
    }

?>
4

2 に答える 2

0

問題はあなたのclsSessionクラスにあるようです。これは を使用して&います。セッション オブジェクトはシリアル化されているため、これらの参照は正しく保存されません。これらを削除してみて (つまり、from パラメータを変更UpdateSessionClassDataUpdateSessionて削除&)、これで問題が解決するかどうかを確認してください。

于 2013-01-18T00:51:51.757 に答える
-1

session_start();開始し、前に置き、デバッグ用require_onceに追加します。var_dump($_SESSION)

于 2013-01-18T00:32:18.513 に答える