note 23908 added to function.session-set-save-handler

From: Date: Wed, 31 Jul 2002 16:19:53 +0000
Subject: note 23908 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-34050@lists.php.net to get a copy of this message
I wrote this because I needed the actual variables being written with a session write... The data that is sent to sess_write is a strange mix of keys and serialized data. Here's a function to unserialize that data: function unserialize_session($val) { // not entirely foolproof, but replace the pipe with something obscure $replacer = chr(254).chr(254).chr(254); $repRegex = '/(")([A-Za-z0-9_]*?)(\|)/'; $val = preg_replace($repRegex,'$1$2'.$replacer,$val); // split the semi-serialized data into keys and values $splitRegex = '/([^;}][A-Za-z0-9_]*?)\|/'; $splitArray = preg_split($splitRegex, $val, -1, PREG_SPLIT_DELIM_CAPTURE); // remove offset 0 -- always empty array_shift($splitArray); // build a useful array $usefulArray = array(); while ($var = array_shift($splitArray)) { $val = array_shift($splitArray); $val = str_replace($replacer, '|', $val); $usefulArray[$var] = unserialize($val); } return $usefulArray; } Basically, it turns a string of session data like: test|a:2:{i:0;s:5:"test1";i:1;s:5:"test2";}test2|s:3:"m|oo"; into: Array ( [test] => Array ( [0] => test1 [1] => test2 ) [test2] => m|oo ) I haven't completely tested this, so it may need some debugging/tweaking. Caveat Lector. S -- http://www.php.net/manual/en/function.session-set-save-handler.php http://master.php.net/manage/user-notes.php?action=edit+23908 http://master.php.net/manage/user-notes.php?action=delete+23908 http://master.php.net/manage/user-notes.php?action=reject+23908

« previous php.notes (#34050) next »