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
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.

Edited by Christian F.
Link to comment
Share on other sites

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.

Link to comment
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.

 

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

Thank you for sharing this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.