Codeigniter会话问题

时间:2012-01-14 08:03:02

标签: php codeigniter authentication

我已经在我的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

提前完成..

3 个答案:

答案 0 :(得分:6)

事实上它们实际上已被注销(正如您所说,刷新会导致它们显示为已注销)。浏览器可能已缓存显示的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

您还可以使用以下代码实现hacky和不建议清除该特定窗口的用户历史记录。这需要作为注销功能的一部分发送到浏览器,如果用户禁用了JavaScript,则无法使用。

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

答案 1 :(得分:1)

我强烈反对禁用缓存,因为这会降低应用的速度。我有同样的问题,因为傻编码,我做的是在我的控制器中,我把以下代码放在每个与数据库交互的函数的顶部,由登录状态的用户使用:

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

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

答案 2 :(得分:0)

是的离子auth有这个问题我在我的应用程序中遇到了同样的问题。 anothr问题是,如果会话在任何链接上到期,它会使您登录页面。但是当您登录并尝试访问会话过期的最后一个链接时,它总会将您带回登录页面。访问清除浏览器缓存所需的页面。这是我在github评论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');
}