Codeigniter:会话到期不起作用

时间:2015-02-07 14:08:08

标签: php codeigniter session

这是我的登录控制器代码:

public function login_validation(){

        $this->load->library('form_validation');
        $this->load->model('model_users');

        $this->form_validation->set_rules('username', 'Username', 'required|trim|alpha_numeric|xss_clean|strip_tags');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|strip_tags');

        if ($this->form_validation->run()){
            if ($this->model_users->can_log_in()){
                $data = array(
                    'username' => $this->input->post('username'),
                    'password' => $this->input->post('password')
                );

                if($this->model_users->get_status($data['username'])){
                    $data = array(
                    'username' => $this->input->post('username'),
                    'privilege' => 'member',
                    'is_logged_in' => 1,
                    'counter'   => 1
                    );
                    $this->session->set_userdata($data);
                    redirect('member');
                } else {
                    $data = array(
                    'username' => $this->input->post('username'),
                    'privilege' => 'admin',
                    'is_logged_in' => 1
                    );
                    $this->session->set_userdata($data);
                    redirect('admin');
                }
            } else {
                $data = array(
                        'error' => 1,
                    );

                $this->session->set_userdata($data);
                redirect('main/login');

            }
        } else {
            $this->login();
        }
    }

这是我的会员控制器代码:

public function __construct(){
        parent::__construct();
        $this->load->library('session');
        if ($this->session->userdata('counter') == 1){
            $data = array(
                    'sessionID' => $this->session->userdata('session_id'),
                    'counter'   => 0
                    );
            $this->session->set_userdata($data);
        }

        if ($this->session->userdata('sessionID') != $this->session->userdata('session_id')){
                $this->session->sess_destroy();
        } else {
                echo "Error";
        }

        if (! $this->session->userdata('is_logged_in')){
            redirect('main/restricted');
        } else {
            $privilege = $this->session->userdata('privilege');
            if ($privilege == 'admin'){
                redirect('main/restricted');
            } else {
                $this->session->sess_expiration = '10';
                //$sessionID = $this->session->userdata('session_id');
                print_r($this->session->all_userdata());
            }
        }
    }

我试图让它变成现在一段时间之后,如果用户处于非活动状态,则在此代码10秒内它们将被注销,但是我在20秒后尝试了它,但会话ID仍然没有改变。 / p>

我认为$this->session->sess_expiration = '10';在等待大约13分钟后没有正常运行,一切正常,会话被重置,用户因为不活动而退出。

但是我希望控制会话到期时间,我能够做到吗?谢谢!

1 个答案:

答案 0 :(得分:1)

如果您希望将会话时间设置为动态,则需要将其设置为手动。

就像我在我的个人网站(CI 3.0)中所做的那样

按照测试示例

if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session and invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}