2015-05-29 79 views
0

所以我有這樣的登錄方法:

public function login(){ 
    $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean'); 
    $this->form_validation->set_rules('password','Username','trim|required|min_length[4]|xss_clean'); 

    if($this->form_validation->run()== FALSE) { 

     //Loading View 
     $this->load->view('admin/layouts/login'); 


     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 

     //Validate Username & Password 
     $user_id = $this->Authenticate_model->login($username, $password); 

     if($user_id){ 
      $user_data = array(
       'user_id' => $user_id, 
       'username' => $username, 
       'logged_in' => true 
      ); 

      //Set session userdata 
      $this->session->set_userdata($user_data); 
     } else { 
      //Set message 
      $this->session->set_flashdata('pass_login', 'You are now logged in'); 
      redirect('admin/dashboard'); 
     } 
    } 
} 

然後我用這個簡單的方法來註銷:

public function logout(){ 
    //Unset User Data 
    $this->session->unset_userdata('user_id'); 
    $this->session->unset_userdata('username'); 
    $this->session->unset_userdata('logged_in'); 
    $this->session->sess_destroy(); 

    redirect('admin/authenticate/login'); 
} 

所以基本上我我解封所有會話userdata然後重新導向到登錄控制器。然後會發生的是,當我重定向回登錄頁面時,我會自動重新登錄,就像我的會話數據仍然有效並存在一樣。爲什麼會發生?

+1

您同時使用未設置並配置了您的設置$這 - >會話級> sess_destroy();嘗試一個或其他? – user4419336

+0

是的,我刪除了'''sess_destroy()'''它工作,但理論上它應該與它一起工作。奇怪。 – Tachi

+0

其實我錯了。它不工作... – Tachi

回答

1

你可以嘗試

unset($this->session->userdata('user_id')); 
unset($this->session->userdata('logged_in')); 
unset($this->session->userdata('username')); 

或只是有

$this->session->sess_destroy(); 

確保您的會議庫自動加載,並根據笨的版本

+1

,因爲'$ this-> session-> userdata('user_id') '返回一個值,你不能在返回的值上調用unset – machineaddict