Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

52 views
Skip to first unread message

se...@php.net

unread,
Jul 31, 2002, 12:19:53 PM7/31/02
to php-...@lists.php.net
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

0 new messages