【问题标题】:Codeigniter Session cannot be closedCodeigniter 会话无法关闭
【发布时间】:2013-10-22 19:17:11
【问题描述】:

我正在使用 codeigniter 开发应用程序。 在这个应用程序中,当用户单击注销按钮时,我取消了会话,但是当我单击浏览器中的后退按钮时,我得到了最后一个注销的页面。

如何解决这个问题?

【问题讨论】:

  • 你需要检查每个控制器的构造函数的会话
  • 我认为这将是一个漫长的过程。只需检查我已经在下面提供的解决方案:D
  • 如果未设置,您可以检查用户会话而不是重定向到登录页面
  • if ( $this->_checkLogin() == false ) { redirect( 'login' ); } function _checkLogin(){ if (!$this->session->userdata('logged_in')) { return false ; } 其他 { 返回真; } } 我试过了,但它不起作用

标签: php codeigniter session


【解决方案1】:

我已经有这种东西了,我做的是这样的:

在您的 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'); }
}

尝试创建一个这样的函数。如果您的控制器,只需在每个构造上调用它。

希望这能启发你:)

【讨论】:

    【解决方案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');
     }
    }
    

    这解决了“返回”按钮问题,也有助于抵御 CSRF 攻击

    【讨论】:

    • @Serma ?那个怎么样?我敢肯定你甚至没有实现它,你不能在不重新发送帖子数据的情况下从 POST 请求返回
    • [code] 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 { 重定向('任务/注销'); } } public function logout(){ if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 销毁会话 $this->session->sess_destroy(); // 重定向到其他页面 $this->load->view('login'); } } 请检查我的代码
    • cmets 中的代码不可读。另外,您是否使用了带有 post 方法的表单?而且我在您的代码中看不到重定向(您只是在加载视图,这是完全不同的事情),这完全没有抓住重点……它是 POST - REDIRECT - GET
    【解决方案3】:
              ##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());
            }
    

    试试这个。它会解决“返回”按钮问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2011-12-02
      • 2017-05-16
      • 1970-01-01
      相关资源
      最近更新 更多