[Php] serialize, unserializethesessiondatainPHP

Source: Internet
Author: User
[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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.