RobertP Posted December 3, 2012 Share Posted December 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/ Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 (edited) 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 December 3, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/#findComment-1397092 Share on other sites More sharing options...
requinix Posted December 3, 2012 Share Posted December 3, 2012 Solution to what? Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/#findComment-1397095 Share on other sites More sharing options...
RobertP Posted December 4, 2012 Author Share Posted December 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/#findComment-1397383 Share on other sites More sharing options...
RobertP Posted December 4, 2012 Author Share Posted December 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/#findComment-1397384 Share on other sites More sharing options...
Christian F. Posted December 4, 2012 Share Posted December 4, 2012 You're welcome, glad I could help. Quote Link to comment https://forums.phpfreaks.com/topic/271523-sessionhandlerinterface-unserialize/#findComment-1397423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.