8

我有一个需要登录的网页。一旦用户登录,我就会启动会话,一旦他注销,我就会将其销毁,但是当我按下后页时,它会再次给我用户个人资料页面,理想情况下,用户已注销时不应出现这种情况。但是,如果我在注销后重新加载页面,它工作正常。

这是一个本地聊天室,在线和登录的每个人都可以一起聊天。共有三个页面:login.php, auth.php,logout.php

login.php是包含表单的常见登录页面。auth.php到目前为止,有一个div显示所有以前的聊天记录,一个文本框和一个共享按钮,单击该按钮再次将表单发送到 auth.php,因此每次发布表单时,聊天帖子都会发送到数据库,并且使用聊天中的最新数据库重新加载身份验证分区..

现在的问题是,一旦我注销,我取消设置所有变量并销毁会话,但即使我在浏览器(Safari)中点击后退按钮,auth.php也可以看到没有最后一个聊天条目的先前版本,理想情况下不应该作为会话是被摧毁。我已经在 中进行了会话验证auth.php,所以基本上我希望auth.php重新加载用户在注销后访问它,因为重新加载auth.php显示“您尚未登录”

我努力了

<?php header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>
and
<head>
<meta http-equiv='Pragma' content='no-cache'>
<meta http-equiv='Expires' content='-1'>
</head>

很抱歉这个冗长的问题,但我真的需要帮助。

4

6 回答 6

12
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); //Date in the past
header("Cache-Control: no-store, no-cache, must-revalidate"); //HTTP/1.1

这适用于我在 FF3.6、IE7、IE8、Chrome5 上(当点击浏览器的后退按钮时,页面总是重新加载

另一种解决方案是发送与函数发送的完全相同的标头session_start(),与上面的非常相似,但更丰富:

header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); //Date in the past
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); //HTTP/1.1
header("Pragma: no-cache");

注意:这里显示的顺序也与发送的顺序相同session_start()(您可以使用FF 的HttpFox插件对此进行测试)

于 2011-05-22T16:12:15.797 回答
7

这些标头将强制浏览器和代理(如果有)不缓存页面并强制向服务器发出新请求以获取该页面:

  header("Cache-Control: private, must-revalidate, max-age=0");
  header("Pragma: no-cache");
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // A date in the past
于 2010-09-05T09:33:39.373 回答
4

对于纯 javascript 解决方案,只需将其添加到您的 HTML:

<input id="alwaysFetch" type="hidden" />
<script>
    setTimeout(function () {
        var el = document.getElementById('alwaysFetch');
        el.value = el.value ? location.reload() : true;
    }, 0);
</script>

该代码只是为隐藏的输入分配一个值,该输入将在单击后退按钮后保留。然后使用它来确定页面是否过时,在这种情况下会重新加载。

于 2014-06-03T13:57:07.457 回答
0

在不更改 url 的情况下,将其放在 head 标记中。
该脚本使用两个 cookie:
- 一个用于存储页面是否已被访问
- 一个用于存储是否触发了重新加载

if(navigator.cookieEnabled){    
    var visited=0;
    var refreshing=0;
    if(Get_Cookie('visited')) {
        visited=parseInt(Get_Cookie('visited'));
    }
    if (Get_Cookie('refreshing')) {
        refreshing=parseInt(Get_Cookie('refreshing'));
    }
    if(visited==1){
        if(refreshing==0){
             window.document.cookie = 'refreshing=1';
             window.location.reload();
        }else{
             window.document.cookie = 'refreshing=0';
        }
    }else{
        window.document.cookie = 'visited=1';
    }
}
于 2012-03-11T19:35:11.027 回答
0

我正在使用混合解决方案,因为我没有找到跨浏览器的方法来在点击后退按钮后重新加载页面。

首先,我在所有页面中添加了一些元标记:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">

其次,我在所有页面的标签上添加了以下 javascript:

<script type="text/javascript">    
// If persisted then it is in the page cache, force a reload of the page.
        window.onpageshow =function(event){
            if(event.persisted){        
                document.body.style.display ="none";        
                location.reload();
            }
        };
</script>

我正在我的一个网站中应用此修复程序,它在 IE 10、Safari 和 Chrome 中运行良好。

于 2014-11-30T06:52:57.560 回答
0

我尝试了很多解决方案,但对我有用的是这行简单的代码。

<body onunload="">

添加带有 body 标签的卸载事件并检查您的问题。

于 2015-10-26T12:40:27.917 回答