sql server - PHP: Custom Session Handler Unserialize Not Working on Windows -


the development environment of application zend framework 1.11, mssql server 2012, sqlsrv extension database connectivity pdo, windows 7, iis 7. session being stored in database using custom session handler class.

the issue when print $_session after session_start() written in bootstrap.php file, not show complete unserialized array of session data. seems session data being returned "read" method of session class handler, not being unserialized reason. here source code:-

bootstrap.php

protected function _initsession() {     $handler = new mcd_core_sessionhandler_database();      session_set_save_handler(         array($handler, 'open'),         array($handler, 'close'),         array($handler, 'read'),         array($handler, 'write'),         array($handler, 'destroy'),         array($handler, 'gc')     );             session_start();     echo "<pre>";     print_r($_session); } 

sessionhandler class

<?php class my_core_sessionhandler_database {      protected $dbh;     protected $ipaddress = '';      public function __construct() {            $this->dbh = my_core_config::get_dbinstance();     }      public function open($savepath, $sessionid) {         return true;     }      public function close()  {         return true;     }      public function read($sessionid) {                          $query = "select top 1 cast(session varchar(max)) session             t_php_session             php_session_id = '".$sessionid."'                 , dateadd(minute, ".my_core_config::get_value('session_timeout_period').", created) >= getdate()                 , user_ip = '".$this->ipaddress."'";          $stmt = $this->dbh->prepare($query);         $stmt->execute();         $result = $stmt->fetchcolumn();                 if ($result === false) {             return '';         }         return $result;     }      public function write($sessionid, $sessiondata) {         $query = "             select id             t_php_session             php_session_id = ?         ";         $stmt = $this->dbh->prepare($query);         $stmt->execute(array($sessionid));         $result = $stmt->fetchcolumn();          if (empty($result)) {                         $query = "insert t_php_session (php_session_id, session, user_ip, created) values (?, convert(varbinary(max), ?), ".$this->ipaddress.", getdate())";             $stmt = $this->dbh->prepare($query);                         $stmt->execute(array($sessionid, $sessiondata));         } else {             $query = "                 update t_php_session                 set session = convert(varbinary(max), ?),                     user_ip = ".$this->ipaddress.",                     created = getdate()                 id = ?             ";             $stmt = $this->dbh->prepare($query);             $stmt->execute(array($sessiondata, $result));         }         return true;     }      public function destroy($sessionid) {         $query = 'delete t_php_session php_session_id = ?';         $stmt = $this->dbh->prepare($query);         $stmt->execute(array($sessionid));          return true;     }      public function gc($maxlifetime) {         $query = "             delete t_php_session             dateadd(minute, ".my_core_config::get_value('session_timeout_period').", created) < getdate()";          $stmt = $this->dbh->prepare($query);         $stmt->execute();          return true;     } } 

the datatype of session field in database table varbinary (max). guess might issue not sure.

output of read method of session handler class is:

my_core_session|a:1:{s:16:"my_core_session";a:4:{s:19:"previous_controller";n;s:15:"previous_action";n;s:18:"current_controller";s:16:"authentification";s:14:"current_action";s:5:"login";}} 

output of $_session in bootstrap.php is:

array (     [my_core_session] =>  ) 

where output should be:

array (     [my_core_session] => array         (             [my_core_session] => array                 (                     [previous_controller] =>                      [previous_action] =>                      [current_controller] => authentification                     [current_action] => login                 )          )  ) 

this application working fine on linux same database, source code being used different environment apache 2.2, freetds, dblib. moved application linux windows , facing issue.

i found solution , share people may face same problem. issue php engine not able deserialize session data being fetched mssql server database may garbage coming along data. data type of field (where session being saved) varbinary (max). changed varchar (max) see if works , worked :)

this specific use case custom session handler used in zend framework mssql server database. luckily, resolved :)

good luck !!


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -