Jump to content

Sessionhandlerinterface Unserialize


RobertP

Recommended Posts

Just wondering if anyone has found a solution to the unserialization issue with the session data saved using the SessionHandlerInterface.

 

http://php.net/manual/en/function.session-decode.php

Please note the unserialization method is not the same as unserialize(). The serialization method is internal to PHP and can be set using session.serialize_handler.

 

Looking through the comments on this page, I am yet to find a viable, and reliable solution.

Link to comment
https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/
Share on other sites

Here's a function I've written to do that:

/**
* Loads the selected session from disk, without starting the session-management.
*
* @param string $SessID
* @return void
* 
* @author Fagerheim Software
* @copyright Protected by Norwegian Law (Åndsverksloven)
* @link www.fagsoft.no
*/
function Sess_Read ($SessID) {
   // Read the session-data from disk
   $Content = file_get_contents ('/var/lib/php5/sess_'.$SessID);

   $Matches = array ();
   $RegExp = '/(\\w+)\\|(\\w:\\d+:.*?;)/';

   $Count = preg_match_all ($RegExp, $Content, $Matches, PREG_SET_ORDER);

   $Content = "a:$Count:{";
   foreach ($Matches as $Line) {
       $Content .= 's:'.strlen($Line[1]).':"'.$Line[1].'";'.$Line[2];
   }

   $_SESSION = unserialize ($Content.'}');
}

 

I wrote this for an AJAX chat client, which was seeing a lot of traffic (several million hits a day). So it was extremely important for it to be as lean and efficient as possible, which is the only reason I bypassed the normal session management.

Solution to what?

 

Where ever you have your sessions stored, file, db, memory, etc.. the session data (_SESSION array) is stored as a string, similar to serialize, but its not at the same time (reefer to first post). I need to parse this back into an array for use.

Here's a function I've written to do that:

/**
* Loads the selected session from disk, without starting the session-management.
*
* @param string $SessID
* @return void
* 
* @author Fagerheim Software
* @copyright Protected by Norwegian Law (Åndsverksloven)
* @link www.fagsoft.no
*/
function Sess_Read ($SessID) {
   // Read the session-data from disk
   $Content = file_get_contents ('/var/lib/php5/sess_'.$SessID);

   $Matches = array ();
   $RegExp = '/(\\w+)\\|(\\w:\\d+:.*?;)/';

   $Count = preg_match_all ($RegExp, $Content, $Matches, PREG_SET_ORDER);

   $Content = "a:$Count:{";
   foreach ($Matches as $Line) {
       $Content .= 's:'.strlen($Line[1]).':"'.$Line[1].'";'.$Line[2];
   }

   $_SESSION = unserialize ($Content.'}');
}

 

I wrote this for an AJAX chat client, which was seeing a lot of traffic (several million hits a day). So it was extremely important for it to be as lean and efficient as possible, which is the only reason I bypassed the normal session management.

 

This is by far the best implementation i have come across!

Thank you for sharing this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.