【问题标题】:CodeIgniter - Could still access the member's page after logging out when back button is clickedCodeIgniter - 单击后退按钮后注销后仍然可以访问会员页面
【发布时间】:2013-11-08 06:26:19
【问题描述】:

我正在使用 CodeIgniter

登录会员后,点击退出后,仍然可以查看会员页面 我单击“返回”按钮。

问题 有谁知道,如果用户退出了,即使点击返回按钮也无法访问会员页面?

请看下面我的代码..

谢谢..

    class Site extends CI_Controller{

    function index()
    {   
        if(!isset($this->session->userdata['is_loggged_in'])) 
            $this->login(); 
        else 
            $this->_template();      
    }

    function login()
    { 
        $data['main_content'] = 'login_form';
        $data['page_title']   = 'Login - php';
        $this->load->view('include/template' , $data);  
    }

    function validate()
    {  
        $this->load->library('form_validation'); 
        $this->form_validation->set_rules('username' , 'Username' , 'trim|required' );
        $this->form_validation->set_rules('password' , 'Password' , 'trim|required' ); 

        if($this->form_validation->run() == FALSE)
        {
            $this->login();
        } 
        else
        { 
            $this->load->model('member_model' , 'member');
            $query = $this->member->checklogin(); 

            //login successful , make sure no duplicate entry during registration
            if($query->num_rows() == 1)
            {
                $data = array(
                    'is_loggged_in' => TRUE ,
                    'username'      => $this->input->post('username'), 
                );
                $this->session->set_userdata($data);
                $this->_template();
            }
            else
            { 
                $data = array(
                        'main_content' => 'login_form' ,
                        'page_title'   => 'Error login' ,
                        'page_error'   => 'Invalid Username or password. '

                );
                $this->load->view('include/template' , $data); 
            }

        }
    }

    function _template()
    { 
        $data = array(
            'main_content' => 'include/default_inc',
            'page_title'   => 'Welcome home ran'
        ); 

        $this->load->view('include/template' , $data);  
    }

    function logout()
    { 
        $this->session->sess_destroy();
        redirect('site/login');
        exit;
    }
  }

/* end of controller site */

【问题讨论】:

  • 在单击注销后检查您的会话是否被破坏。此外,在每个页面中添加一个检查是否设置了会话,否则重定向到登录
  • 验证 $_SESSION 的状态以确认它正在被销毁。您可以使用 print_r($_SESSION)。
  • Portu,CI 不使用 $_SESSION 变量。
  • @Fibbe 感谢您的提醒,我没有机会与 CI 合作,因此非常感谢您的意见。我认为这只是检查会话状态的问题,但看起来幕后不仅仅是一个 $_SESSION 变量。
  • 单击back 按钮不会重新加载页面。无论您如何退出,您仍然可以访问上一页。但是,如果您刷新/重新加载页面,您将被带到登录页面

标签: php codeigniter


【解决方案1】:

不要在索引函数中检查会话,而是尝试在构造函数中检查它

function __constuctor()
{   
    parent::__construct();
    $session = $this->session->userdata('is_loggged_in');
    if ( !$session) {
        $this->login(); 
    }else{
        $this->_template();    
    }     
}

【讨论】:

    【解决方案2】:

    基本上,您的浏览器会返回一个页面并简单地复制它当时收到的 HTML。有很多方法可以解决这个问题,我保证您可以通过少量搜索轻松找到它们。

    防止这种情况的一种简单方法是在页面加载时运行 ajax,检查您是否已登录,如果未登录则更新页面。并且 ajax 将始终运行,即使通过使用 Back。

    controllers/check_login.php

    <?
    
    class Check_login {
    
        function index()
        {
            $bool = (bool) $this->session->userdata('is_loggged_in');
    
            $this->output->set_content_type('application/json');
            if ($bool)
                $this->output->set_status_header('500');
    
            $arr_json = array( 'need_login' => $bool);
    
            echo json_encode($arr_json);
        }
    
    }
    

    jQuery javascript

    $(function() {
    
        $(document).ajaxError(function(event, jqXHR) {
    
            var data    =   jqXHR.responseJSON;
    
            if (data.need_login)
                if (confirm(data.message))
                    window.location = window.location.href;
    
           $.get('/check_login');
    
        });
    
    });
    

    注意!这种方法还可以通过巧妙地使用$this-&gt;input-&gt;is_ajax_request()'need_login' 值,为您提供一种处理无效ajax 调用的简单方法。

    【讨论】:

    • 这就是我做一些改变的方式。它很好地解决了上述问题。投票赞成
    【解决方案3】:

    假设您的会话已成功销毁,但仍可访问前一页(来自浏览器的历史记录),那么您可以通过使用每个 controllers constructor 中的代码来解决此问题。我建议扩展核心 Output Library。

        $this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
        $this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
        $this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
        $this->output->set_header('Pragma: no-cache'); 
    

    【讨论】:

      【解决方案4】:

      从您的代码中,您已注销,即使是 html 页面也被浏览器缓存,因此会出现此类问题。我正在提供一个经过良好测试的解决方案。 . 将以下代码放在您的助手中的某处(比如说 my_helper.php)。

      function no_cache()
      {
          header("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
          header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
          header("Cache-Control: no-store, no-cache, must-revalidate");
          header("Cache-Control: post-check=0, pre-check=0", false);
          header("Pragma: no-cache");
      }
      

      加载帮助器并在控制器的构造函数中调用 no_cache 函数

      public function __construct()
      {
          parent::__construct();
          $this->load->helper('my_helper');
          no_cache();
      
      }
      

      【讨论】:

        【解决方案5】:

        这就是逻辑

        如果你在所有控制器中禁用缓存,它会起作用,但它很愚蠢。也许它会减慢您的网络负载,甚至影响您的 SEO。

        所以解决的办法就是在每个页面中检查用户状态,如果他的状态是登录则禁用缓存,否则什么都不做

        这是我在每个函数中放入的代码示例部分

        if ($this->session->userdata('logged_in')){
             $this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
             $this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
             $this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
             $this->output->set_header('Pragma: no-cache');}
        

        之后不需要添加 else 语句,只需将其余代码放入即可。

        因此,用户登录 id 并浏览您的网络时,没有存储缓存。尝试注销并点击返回按钮。我敢打赌它有效

        【讨论】:

          猜你喜欢
          • 2020-06-18
          • 2014-05-13
          • 2021-10-17
          • 1970-01-01
          • 2021-11-02
          • 2012-05-12
          • 2016-01-20
          • 2012-05-07
          • 1970-01-01
          相关资源
          最近更新 更多