用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2406|回复: 0
收起左侧

[HELP] CI中的session类对不同变量设置不同的保存时间的问题

[复制链接]
发表于 2012-8-29 20:36:53 | 显示全部楼层 |阅读模式
本帖最后由 小白TO大白 于 2012-8-30 14:57 编辑

  之前用CI的session类来保持数据,更改了config.php里面的设置时间为一个星期。然后今天突然想设置一个保持时间为1分钟的变量,但发现没有提供参数。就去看了下源码

PHP复制代码
/** 然我我自己在后面加了一个expire参数来保持时间         * Add or change data in the "userdata" array
         *
         * @access        public
         * @param        mixed
         * @param        string
         * @return        void
         */

        function set_userdata($newdata = array(), $newval = '', $expire = 0)
        {
                if (is_string($newdata))
                {
                        $newdata = array($newdata => $newval);
                }
 
                if (count($newdata) > 0)
                {
                        foreach ($newdata as $key => $val)
                        {
                                $this->userdata[$key] = $val;
                        }
                }
 
                $this->sess_write($expire);
        }
 
复制代码


然后再来看sess_write,因为不存数据库,所以直接调用_set_cookie(),把我们的参数expire也带上
PHP复制代码
/**         * Write the session data
         *
         * @access        public
         * @return        void
         */

        function sess_write($expire = 0)
        {
                // Are we saving custom data to the DB?  If not, all we do is update the cookie
                if ($this->sess_use_database === FALSE)
                {
                        $this->_set_cookie($expire);
                        return;
                }
 
复制代码


最后看到_set_cookie函数
PHP复制代码
/**         * Write the session cookie
         *
         * @access        public
         * @return        void
         */

        function _set_cookie($expire = 0, $cookie_data = NULL)
        {
                if (is_null($cookie_data))
                {
                        $cookie_data = $this->userdata;
                }
 
                // Serialize the userdata for the cookie
                $cookie_data = $this->_serialize($cookie_data);
 
                if ($this->sess_encrypt_cookie == TRUE)
                {
                        $cookie_data = $this->CI->encrypt->encode($cookie_data);
                }
                else
                {
                        // if encryption is not used, we provide an md5 hash to prevent userside tampering
                        $cookie_data = $cookie_data.md5($cookie_data.$this->encryption_key);
                }
               
                if($expire)
               {
                         $expire = $expire + time();
               }
               else
               {
                         $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();
               }
                // Set the cookie
                setcookie(
                                        $this->sess_cookie_name,
                                        $cookie_data,
                                        $expire,
                                        $this->cookie_path,
                                        $this->cookie_domain,
                                        $this->cookie_secure
                                );
        }
 
复制代码


其实CI的session是存的cookie数据,然后用一个键名包含所有数据,最后取出来,再解析分成多个你存储的数据。查看浏览器cookie中只有一个值就可以看出。在这里,每次设置cookie用的都是同一个sess_cookie_name,所以如果你再次设置一个新的时间为1分钟的话,1分钟之后,之前设置为1周的也就同时消失了。
    我也是新手,不知道说的对不对?希望大家指正

本版积分规则