[php] serialize, unserialize the session data in PHP
2012-02-13 09:23
676 查看
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 to
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;
}
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 to
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;
}
相关文章推荐
- php-curl ErrnoProblem (2) in the Chunked-Encoded data问题解决
- Decoupling models from the database: Data Access Object pattern in PHP
- Sesing the Data Structures & Algorithms in Java
- php 使用serialize() 和 unserialize() 让对象成超级变量
- PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files)
- 130.View the Exhibit and examine the data in the EMPLOYEES table:
- CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data'
- [Deployer:149163]The domain edit lock is owned by another session in non-exclusive mode
- Security and Privacy for Cloud-Based Data Management in the Health Network Service Chain: A Microser
- how to get the function back trace in php
- Overloading the Array Access Syntax in PHP
- 解决cakePHP中的. session_start()[function.session-start];open_basedir restnction in effect.File(c:/windows/temp/)is not within the a
- php serialize(),unserialize()
- Notice: unserialize(): Error at offset 134526789 of 2136547489 bytes in file.php on line 130
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- Resumable Media Uploads in the Google Data Protocol
- Ember Data- Get a Model in the Console
- --initialize specified but the data directory has files in it. Aborting
- What are the practical differences between SVD and wavelet transforms in data analysis?
- The page you requested, favicon.ico, could not be found. in file **/system/core/Kohana.php on line 841