Converting $ session data to an array?

In the write function for the session persistence handler, $ data is passed in the following format:

test | a: 1: {s: 3: "Foo"; s: 3: "bar";} sessions | a: 2: {s: 10: "isLoggedIn"; b: 1; s: 8: "ClientID"; s: 5: "12345";}

Is there a way to convert this to a valid array, which would be the following:

array ( 'test' => array ( 'foo' => 'bar' ) 'session' => array ( 'isLoggedIn' => true 'clientId' => '12345' ) ) 

I tried passing this to unserialize, but I get an error:

unserialize () [function.unserialize]: error with offset 0 out of 95 bytes

and it just returns false.

+7
source share
2 answers
+3
source

about another answer. The description of session_decode is "session_decode (), decodes the session data in the data by setting the variables stored in the session." It doesn't look like it does what you need ... and also it will always return bool after parsing the string.

on the other hand, if the line you provided as an example had an error, a space after "12345" (and it looks like an error, because before it you see that the next value should be a line with length 5), you can use this function:

 function unserialize_session_data( $serialized_string ) { $variables = array(); $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); for( $i = 0; $i<count($a); $i = $i+2 ) { if(isset($a[$i+1])) { $variables[$a[$i]] = unserialize( $a[$i+1] ); } } return( $variables ); } 
+7
source

All Articles