点击后退按钮重新加载页面

时间:2010-09-05 09:30:29

标签: php caching back-button

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

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

login.php是包含表单的常见登录页面。 auth.php有一个div显示以前的所有聊天记录,点击一个文本框和共享按钮,表单再次发送到auth.php,因此每次发布表单时,聊天记录都会发送到数据库并且auth重新加载了聊天div中的最新数据库..

现在问题是我退出后我取消设置所有变量并销毁会话但是如果我点击浏览器(Safari)中的后退按钮,则会看到之前版本的auth.php没有上一个聊天条目理想情况下不应该破坏会话。我在auth.php中进行了会话验证,所以基本上我希望auth.php重新加载用户在注销后重新加载它,因为重新加载auth.php显示“你还没有登录”< / p>

我试过了

<?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>

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

6 个答案:

答案 0 :(得分:10)

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() 发送的订单的相同(您可以使用HttpFox插件进行测试FF)

答案 1 :(得分:6)

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

  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

答案 2 :(得分: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>

代码只是为单击后退按钮后保留的隐藏输入赋值。然后用它来确定页面是否陈旧,在哪种情况下重新加载。

答案 3 :(得分:0)

不更改网址,将其放在头标记中 该脚本使用两个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';
    }
}

答案 4 :(得分: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中运行良好。

答案 5 :(得分:0)

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

<body onunload="">

使用正文标记添加卸载事件并检查您的问题。

相关问题