2015-02-07 56 views
0

這是我的登錄控制器代碼:笨:會話過期不工作

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沒有改變。

我在考慮$this->session->sess_expiration = '10';在等待大約13分鐘後無法正常工作,一切正常,一切正常,會話被重置並且用戶因非激活而退出。

但是我想控制會話過期的時間,我能做到嗎?謝謝!

+0

可能導致:https://stackoverflow.com/questions/27557216/why-code- igniter-session-time-expiration-not-calculation-from-last-user-activity/28090384?noredirect = 1#comment44766936_28090384 – AdrienXL 2015-02-07 14:13:11

+0

Session $ this-> session-> sess_expiration ='10';不能像那樣在你的config.php中設置超時會話 – user4419336 2015-02-08 04:01:00

+0

哦,我明白了,所以我不能動態地設置它。 – Outliers 2015-02-08 06:33:17

回答

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 
}