2013-10-15 84 views
0

我正在開發使用codeigniter的應用程序。 在這個應用程序中,當用戶點擊註銷按鈕時,我取消了會話設置,但是當我在瀏覽器中單擊後退按鈕時,我正在接收最後註銷的頁面。Codeigniter Session無法關閉

如何解決這個問題?

+0

你需要在每一個控制器的構造函數 –

+0

檢查會議,你能告訴我怎麼樣? – Serma

+0

我認爲這將是一個漫長的過程。只是檢查我的解決方案「已經alerady提供如下:d – Vainglory07

回答

0

我已經這樣的事,我所做的是這樣的:在你的htaccess

<IfModule mod_headers.c> 
Header add Cache-Control: "no-store, no-cache, must-revalidate" 
</IfModule> 

我與你的問題的想法是,你必須自動清除緩存,以便一旦你取消會議,你就不能回到上一頁(我的意思是查看最後一頁)。

同樣的想法,如果你想在php中做到這一點。

/* content security */ 
function weblock() { 
    $ci =& get_instance(); 
    $ci->load->library('session'); 
    $ci->load->model('mlogin'); 

    // clear cache to prevent backward access 
    $ci->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0"); 
    $ci->output->set_header("Pragma: no-cache"); 

    // prevent unauthenticated access 
    if($ci->session->userdata('user_data')==FALSE) { redirect('clogin/logout');} 
    // prevent invalid authentication 
    if(!$ci->mlogin->authenticate()) { redirect('clogin/logout'); } 
} 

試着創建一個像這樣的函數。如果你的控制器在每個結構上調用它。

希望這啓發你:)

+0

對不起。這也沒有爲我工作 – Serma

+0

我更新了我的答案:D – Vainglory07

2

一種解決方案是使用POST和圖案PRG(POST-REDIRECT-GET):

創建註銷按鈕:

<?php echo form_open('logout');? 
<button type="submit">Logout</button> 
<?php echo form_close();?> 

在您的控制器中:

public function logout{ 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // destroy session 
    $this->session->sess_destroy(); 
    // redirect to other page 
    redirect('login', 'refresh'); 
} 
} 

這樣可以解決「後退」按鈕問題,還有助於防止CS RF攻擊

+0

我喜歡這個..:D – Vainglory07

+0

對不起,這個答覆沒有解決我的問題 – Serma

+0

@Serma?那個怎麼樣?我確信你甚至沒有實現它,你不能從POST請求回去而不重新發送數據 –

0
  ##Add this Code in Constructor ## 
    ## start Constructor ## 
      //********** Back button will not work, after logout **********// 
     header("cache-Control: no-store, no-cache, must-revalidate"); 
     header("cache-Control: post-check=0, pre-check=0", false); 
     // HTTP/1.0 
     header("Pragma: no-cache"); 
     // Date in the past 
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
     // always modified 
     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
    //********** Back button will not work, after logout **********// 
    ## End Constructor ## 

public function index(){ 

     redirect('home/logout'); 
    } 

public function home() { 

       $this->form_validation->set_rules('username', 'User', 'trim|required'); 
       $this->form_validation->set_rules('password', 'Password', 'trim|required'); 
       if($this->form_validation->run() AND $data['records'] =$this->task_model->check_login()) 
        { 
        $this->session->set_userdata('logged_in',TRUE); 
        $this->load->view('home'); 
        } 
        else { 
         redirect('task/logout'); 
         } 
        } 

     public function logout(){ 
      $this->session->unset_userdata('userid'); 
      $this->session->unset_userdata('username'); 
      $this->session->destroy(); 
      redirect(base_url()); 
     } 

試試這個。它將解決了「後退」按鈕問題