在codeigniter dyanamicly会话超时

时间:2014-01-02 05:07:43

标签: php codeigniter codeigniter-2

我有关于codeigniter Timeout的问题。

我手动知道配置文件夹设置会话超时,如l

$config['sess_expiration']      = 123;

但我需要网站管理员管理会话时间dyanamicly进入管理页面

请帮我解释如何实现这个逻辑

我试过这个逻辑但没有工作

$this->session->sess_expiration = "120";

注意:这里我将数据库存储在值中。基于我可以设置到会话到期时间的数据库值

注1:$config['sess_time_to_update'] = 30;此值小于会话到期时间

总逻辑代码:

public function edit($id)
    {
        Assets::add_css('../plugins/forms/uniform/uniform.default.css');
        Assets::add_css('../plugins/forms/select/select2.css');
        Assets::add_css('../plugins/forms/validate/validate.css');
        Assets::add_css('../plugins/misc/qtip/jquery.qtip.css');
        Assets::add_js('../plugins/charts/sparkline/jquery.sparkline.min.js');
        Assets::add_js('../plugins/forms/uniform/jquery.uniform.min.js');
        Assets::add_js('../plugins/forms/select/select2.min.js');
        Assets::add_js('../plugins/forms/validate/jquery.validate.min.js');
        Assets::add_js('../plugins/forms/wizard/jquery.bbq.js');
        Assets::add_js('../plugins/forms/wizard/jquery.form.js');
        Assets::add_js('../plugins/forms/wizard/jquery.form.wizard.js');
        Assets::add_module_js('setting','setting'); 
        if ($_POST)
       {
            $current_date = date("Y-m-d H:i:s");
            $data = array(
                            's_meta_value' => $this->input->post('s_meta_value'),
                            'updated_on' => $current_date
                          );
            $this->setting_model->session_mng_update($data,$id);
            $session_val=  $this->input->post('s_meta_value');
           if($session_val == 0)
           {
             $this->session->sess_expiration = '0';
           }
           else
           {          

            $this->session->sess_expiration = "120";
          // $val1 = $this->config->item('sess_expiration');
       // print_r($val1);
            //$session_seconds = ($session_val*60);
          $val2 = $this->config->set_item('sess_expiration',50);
          $this->session->CI_Session();
         //$val1=  $this->config->set_item('sess_expiration',50);
            $val3 = $this->config->item('sess_expiration');
             print_r($val3);exit;


           }

            Template::redirect('setting/setting/display');


        } 

       $val3 = $this->config->item('sess_expiration');
        print_r($val3);exit;
        $data = $this->setting_model->session_mng_edit($id);
        Template::set('page_title', 'Edit Session Management');
        Template::set('data', $data);
        Template::set_view('setting/session_management/edit_session_management');
        Template::render();       
}

2 个答案:

答案 0 :(得分:0)

首先,您可以通过$this->input->set_cookie()为此流程设置Cookie。但是如果你想覆盖配置变量,那么试试

$this->config->set_item('sess_expiration',120);

还要调用会话构造函数以通过

更新值
$this->session->CI_Session();

答案 1 :(得分:0)

你应该使用这样的东西:

$remember_me = $this->input->post('remember_me');
if ($remember_me == 'remember_me')
{
    //set session to non-expiring
    $this->session->sess_expiration = '32140800'; //~ one year
    $this->session->sess_expire_on_close = 'false';
    #$this->session->set_userdata($session_data);
}
else
{
    //set session expire time, after that user should login again
    $this->session->sess_expiration = '1800'; //30 Minutes
    $this->session->sess_expire_on_close = 'true';
    #$this->session->set_userdata($session_data);
}

//set session and go to Dashboard or Admin Page
$this->session->set_userdata(array(
    'id' => $result[0]['id'],
    'username' => $result[0]['username']
));