2013-10-15 34 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()); 
     } 

试试这个。它将解决了“后退”按钮问题