[Php] serialize, unserializethesessiondatainPHP As we know, in PHP, we can use session_encode () and session_decode () to encode/decode the session data, but, if you have tried these two
Functionality, you will see the they are not going as you think. So here, I find these two functionality which will help you to do that.
/**
* Serialize session
*
* @ Param array $ data
* @ Param boolean $ safe
* @ Return string
*/
Function serialize_session ($ array, $ safe = true)
{
// The session is passed as refernece, even if you dont want it
If ($ safe)
{
$ Array = unserialize (serialize ($ array ));
}
$ Raw = '';
$ Line = 0;
$ Keys = array_keys ($ array );
Foreach ($ keys as $ key)
{
$ Value = $ array [$ key];
$ Line ++;
$ Raw. = $ key. '| ';
If (is_array ($ value) & isset ($ value ['huge _ recursion_blocker_we_hope '])
{
$ Raw. = 'r: '. $ value ['huge _ recursion_blocker_we_hope']. ';
}
Else
{
$ Raw. = serialize ($ value );
}
$ Array [$ key] = Array ('huge _ recursion_blocker_we_hope '=> $ line );
}
Return $ raw;
}
/**
* Unserialize session
*
* @ Param string $ data
* @ Return array
*/
Function unserialize_session ($ data)
{
$ Vars = preg_split ('/([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff ^ |] *) \ | /', $ data,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
For ($ I = 0; $ vars [$ I]; $ I ++)
{
$ Result [$ vars [$ I ++] = unserialize ($ vars [$ I]);
}
Return $ result;
}