【问题标题】:Codeigniter session problemsCodeigniter 会话问题
【发布时间】:2012-02-10 06:27:48
【问题描述】:

我在我的 codeigniter 应用程序中使用了很棒的codeigniter authentication library ion auth 创建了用户身份验证,身份验证工作正常,但是当我注销并单击浏览器的后退按钮时,我可以浏览我在我的应用程序中访问过的所有页面这引起了对用户隐私的担忧,但是如果我尝试刷新页面,它会识别出我已注销。当用户单击浏览器的后退按钮时,如何强制浏览器重新加载?任何有关如何解决此问题的建议将不胜感激..

编辑

控制器注销功能

function logout() {
    //log the user out
    $logout = $this->ion_auth->logout();

    //redirect them back to the page they came from
    redirect('auth', 'refresh');
}

这是来自ion auth的注销功能

public function logout() {
    $this->ci->ion_auth_model->trigger_events('logout');

    $identity = $this->ci->config->item('identity', 'ion_auth');
    $this->ci->session->unset_userdata($identity);
    $this->ci->session->unset_userdata('group');
    $this->ci->session->unset_userdata('id');
    $this->ci->session->unset_userdata('user_id');

    //delete the remember me cookies if they exist
    if (get_cookie('identity')) {
        delete_cookie('identity');
    }
    if (get_cookie('remember_code')) {
        delete_cookie('remember_code');
    }

    $this->ci->session->sess_destroy();

    $this->set_message('logout_successful');
    return TRUE;
}

我正在使用 codeigniter 2.0.3

提前谢谢..

【问题讨论】:

  • 显示一些代码...我敢打赌你不会在每个页面上检查“已登录”这可能是为什么,只在登录时完成。需要检查您的“安全区域”中的每一页
  • 我确实检查过了,这就是为什么当我重新加载页面时它会将我重定向到登录页面,看起来当有人注销时页面没有重新加载
  • 也许你还没有完全退出,没有任何代码很难说,发布一个典型的控制器设置,因为我在 CI 上的会话中从未遇到过这个问题。
  • 他们实际上已经注销了,但是你能看到的页面仍然是浏览器缓存的页面。
  • @Jakub 我已经编辑了我的问题。我注销,当我尝试单击任何链接或重新加载页面时,这可以证明是被重定向到登录页面。

标签: php codeigniter authentication


【解决方案1】:

它们实际上可能已注销(正如您所说,刷新会导致它们看起来已注销)。很可能浏览器已经缓存了显示的 HTML,表明他们已经登录,但在他们退出后没有重新加载它。

您可以通过设置Cache-Control 标头将有登录相关信息的页面设置为无缓存。

这可以通过 HTML 来实现

<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">

或 PHP

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

您还可以使用以下代码实现清除该特定窗口的用户历史记录的骇人听闻且不明智的做法。这需要作为注销功能的一部分发送到浏览器,如果用户禁用了 javascript,它将无法工作。

<script language="javascript"> 
     var Backlen=history.length;   
     history.go(-Backlen);   
     window.location.href=page url
</SCRIPT>

【讨论】:

  • 非常感谢@Ben Swinburne .. 这是缓存问题。我通过在除身份验证控制器之外的每个控制器的构造函数中添加 $this-&gt;output-&gt;set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0"); $this-&gt;output-&gt;set_header("Pragma: no-cache"); 在 codeigniter 中实现了这一点。
  • 尝试在控制器级别的数据库交互功能处重定向,而不是禁用缓存。
  • @SamSamson:我不明白为什么除了 "authentication controller" 之外的任何地方都没有缓存。我认为 ion_auth 像每个人一样使用会话。所以在“身份验证控制器”中也应该没有缓存。
【解决方案2】:

我强烈反对禁用缓存,因为这会降低您的应用程序的速度。由于愚蠢的编码,我遇到了同样的问题,我所做的是在我的控制器中,我将以下代码放在与数据库交互的每个函数的顶部,供登录状态的用户使用:

//Do not let anyone interact with database from cached pages!
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    }

因此,如果注销用户的浏览器被“备份”到缓存的登录状态并试图摆弄数据库,则重定向到登录页面会发生,这也意味着刷新。

【讨论】:

    【解决方案3】:

    是的 ion auth 有这个问题我在我的应用程序中遇到了同样的问题。另一个问题是,如果会话在任何链接上过期,它会将您带到登录页面。但是当您登录并尝试访问会话过期的最后一个链接时,它总是会带您回到登录页面。要访问您需要清除浏览器缓存的页面。这是我在 github cmets on ion auth 上找到的解决方案

    github ion-auth comment link

    function logout() {
        //log the user out
        $logout = $this->ion_auth->logout();
    
         header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
         header("Expires: Wed, 4 Jul 2012 05:00:00 GMT"); // Date in the past
    
        //redirect them back to the page they came from
        redirect('auth', 'refresh');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2011-10-08
      • 2020-06-23
      相关资源
      最近更新 更多