zoukankan      html  css  js  c++  java
  • [php] serialize, unserialize the session data in PHP

    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;
        }
  • 相关阅读:
    两路归并算法
    个性化搜索引擎调研(三)
    编程珠玑开篇磁盘文件排序问题
    Lucene里经常被聊到的几个话题
    成就霸业的座右铭(绝对经典)
    别人对你的态度,决定了你的命运
    iBATIS缓存实现分析[转]
    Taste/Thoth:Taste Architecture 概览【转Beyond Search】
    中文分词算法笔记
    ConcurrentModificationException主要原因及处理方法
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/2348642.html
Copyright © 2011-2022 走看看