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

[会话/Cookie] 感觉CI会话不爽,重写了会话函数

[复制链接]
发表于 2011-10-28 15:34:14 | 显示全部楼层 |阅读模式
原CI的会话总是莫名其妙的改变会话ID,而我又懒得翻他的session类,也不喜欢像are333那样写一个类,所以我在helper目录下新建了一个mysession_helper.php
PHP复制代码
 
<?php
 
/**
 * author:jeffstric
 * E-mail:jeffstricg@gmail.com
 * blog:jeffsc.info
 * */

if(!function_exists('start_mys')){
    function start_mys($input=null){
        $CI = &get_instance();
        $CI->load->library('Encrypt');
        @session_start();
        if($input!=null){
            if(is_array($input))
                foreach($input as $key=>$value){
                    $_SESSION[$key]=$CI->encrypt->encode($value);
                }
           else
                $_SESSION['value']=$CI->encrypt->encode($input);
        }
        $key=$CI->config->item('encryption_key');
        //密码中除了session_id外还包含了用户浏览器信息和你的config中的密码,可以确保会话安全
        return md5(session_id().$key.$_SERVER['HTTP_USER_AGENT']);
    }
}
 
 
if(!function_exists('get_mys')){
    function get_mys($input=null){
        $CI = &get_instance();
        $CI->load->library('Encrypt');
        @session_start();
        if($input!=null){
            if(is_array($input)){
                $result = array();
                foreach($input as $value){
                    array_push($result,$CI->encrypt->decode($_SESSION[$value]));
                }
                return $result;
            }
           else
                return $CI->encrypt->decode($_SESSION[$input]);
        }
    }
}
 
?>
 
 
 
复制代码

使用起来也很简单
在控制器里添加
PHP复制代码
 
                $this->load->helper('mysession');
                var_dump(start_mys(array('a'=>'b','jeff'=>'qqq')));
 
 
复制代码

即可存储,而添加
PHP复制代码
 
       $this->load->helper('mysession');
       var_dump(get_mys(array('a','jeff')));
 
复制代码

  没什么含量,只是为了帮助你们节省点时间

评分

参与人数 1威望 +5 收起 理由
Hex + 5 赞一个!

查看全部评分

 楼主| 发表于 2011-10-28 21:26:42 | 显示全部楼层
忘记加
PHP复制代码
 
setcookie(ini_get('session.name'),session_id(),time() + 3600);
 
复制代码


发表于 2011-10-28 23:07:31 | 显示全部楼层
{:soso_e102:}
发表于 2011-10-28 23:12:06 | 显示全部楼层
留个名
 楼主| 发表于 2011-11-1 22:13:17 | 显示全部楼层
亲们,不好意思,我后来经过测试发现该方法有些难以名状的问题,特别是如果依然启用CI原生的session类的话,比如会导致生成两个cookie,或者是载入了错误的cookie导致读取错误,总而言之,我提出的方案是不合适的。所以我又退回到了使用CI原生的session类中,但对原生类中做了一些修改
将原生session中的sess_update()函数禁止session_id变化
PHP复制代码
 
function sess_update()
        {
                // We only update the session every five minutes by default
                if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
                {
                        return;
                }
                //禁止session_id变化
                /*
                // Save the old session id so we know which record to
                // update in the database if we need it
                $old_sessid = $this->userdata['session_id'];
                $new_sessid = '';
                while (strlen($new_sessid) < 32)
                {
                        $new_sessid .= mt_rand(0, mt_getrandmax());
                }
 
                // To make the session ID even more secure we'll combine it with the user's IP
                $new_sessid .= $this->CI->input->ip_address();
 
                // Turn it into a hash
                $new_sessid = md5(uniqid($new_sessid, TRUE));
 
                // Update the session data in the session data array
                $this->userdata['session_id'] = $new_sessid;
                */

                $this->userdata['last_activity'] = $this->now;
 
                // _set_cookie() will handle this for us if we aren't using database sessions
                // by pushing all userdata to the cookie.
                $cookie_data = NULL;
 
                // Update the session ID and last_activity field in the DB if needed
                if ($this->sess_use_database === TRUE)
                {
                        // set cookie explicitly to only have our session data
                        $cookie_data = array();
                        foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
                        {
                                $cookie_data[$val] = $this->userdata[$val];
                        }
 
                        $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now), array('session_id' => $this->userdata['session_id'])));
                }
 
                // Write the cookie
                $this->_set_cookie($cookie_data);
        }
 
复制代码

虽然降低了安全性,但至少可以保证通过session_id来判断是否为登陆用户
不过,在本地测试时依然出现一些丢失登陆状态的情况,DEBUGing……
发表于 2011-11-23 15:05:26 | 显示全部楼层
楼主很强呢,看了这么长的代码我都头晕了
发表于 2011-11-24 13:17:22 | 显示全部楼层
留名 支持.........................

本版积分规则