php 点击后退按钮重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3645609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload the page on hitting back button
提问by tushar
I have a webpage that requires login. Once a user has logged in I start the session and once he logs out I destroy it, but when I press the back page it gives me the user profile page again which ideally should not be the case as the user has logged out. However, it works fine if I reload the page after logging out.
我有一个需要登录的网页。一旦用户登录,我就开始会话,一旦他退出,我就会销毁它,但是当我按下后页时,它再次为我提供用户配置文件页面,理想情况下不应该是用户已注销的情况。但是,如果我在注销后重新加载页面,它就可以正常工作。
It's a local chatroom where everybody online and logged in can chat together. There are three pages: login.php
, auth.php
, logout.php
这是一个本地聊天室,在线和登录的每个人都可以在这里聊天。共有三页:login.php
, auth.php
,logout.php
login.php
is the common login page containg a form. auth.php
has a div
displaying all previous chats up til now, a textbox and share button on clicking which a form is sent again to auth.php so everytime the form is posted the chatpost is sent to database and auth is reloaded with the latest database within the chat div..
login.php
是包含表单的常见登录页面。auth.php
有一个div
显示所有以前的聊天直到现在,一个文本框和共享按钮,点击后一个表单再次发送到 auth.php,所以每次发布表单时,聊天帖子都会发送到数据库,并在聊天中使用最新的数据库重新加载 auth分..
Now the problem is once I logout I unset all the variables and destroy the session but even then if I hit the back button in browser (Safari), the previous version of auth.php
without the last chat entry is visible which ideally should not as the session is destroyed. I have put a session validation in auth.php
, so basically I want the auth.php
to reload of the user visits it after logging out as reloading auth.php
displays that "you are not logged in"
现在的问题是,一旦我注销,我就会取消设置所有变量并销毁会话,但即使如此,如果我在浏览器(Safari)中点击后退按钮,auth.php
也可以看到没有最后一个聊天条目的先前版本,理想情况下不应该是会话摧毁了。我已经把会话验证auth.php
,所以基本上我想要的auth.php
用户访问的重载它登出作为重装后auth.php
说:“你是不是在登录”显示器
i have tried
我试过了
<?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>
Sorry for the lengthy question but I really need help on this.
抱歉问了这么长的问题,但我真的需要这方面的帮助。
采纳答案by aularon
These headers will force the browser, and proxies if any, not to cache the page and force a new request to the server for that page:
这些标头将强制浏览器和代理(如果有)不缓存页面并强制向服务器发送该页面的新请求:
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
回答by Marco Demaio
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
This works for me on FF3.6, IE7, IE8, Chrome5 (when hitting browser's back/forth button the page is always RELOADED)
这在 FF3.6、IE7、IE8、Chrome5 上对我有用(当点击浏览器的后退/前进按钮时,页面总是重新加载)
Another solution is to send exactly the same headers sent by session_start()
function, very similar to the ones above, but even more rich:
另一种解决方案是发送与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");
NOTE:also the order shown here is the SAME as the order sent by session_start()
(you can test this with HttpFoxaddon for FF)
注意:此处显示session_start()
的订单也与发送的订单相同(您可以使用FF 的HttpFox插件对此进行测试)
回答by Ziad
For a pure javascript solution just add this to your HTML:
对于纯 javascript 解决方案,只需将其添加到您的 HTML 中:
<input id="alwaysFetch" type="hidden" />
<script>
setTimeout(function () {
var el = document.getElementById('alwaysFetch');
el.value = el.value ? location.reload() : true;
}, 0);
</script>
The code simply assigns a value to the hidden input that will remain after the back button is clicked. This is then used to determined if the page is stale in which case it is reloaded.
该代码只是为隐藏的输入分配一个值,该值将在单击后退按钮后保留。然后使用它来确定页面是否过时,在这种情况下它会被重新加载。
回答by Leon
I'm using a mixed solution, as I did not find a cross-browser method to reload the page after hitting the backbutton.
我使用的是混合解决方案,因为我没有找到在点击后退按钮后重新加载页面的跨浏览器方法。
Firstly, I add some meta tags to all of my pages:
首先,我向所有页面添加了一些元标记:
<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">
Secondly,I add the following javascript on the tag in all of my pages:
其次,我在所有页面的标签上添加以下 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>
I am applying this fix in one of my websites and it is working fine in IE 10, Safari and Chrome.
我正在我的一个网站中应用此修复程序,它在 IE 10、Safari 和 Chrome 中运行良好。
回答by Shwet
I have tried many solutions but what worked for me is this simple line of code.
我尝试了很多解决方案,但对我有用的是这行简单的代码。
<body onunload="">
Add unload event with body tag and check your issue.
添加带有 body 标签的卸载事件并检查您的问题。
回答by Hatzegopteryx
Without changing the url, put this in the head tag.
The script uses two cookies:
- one to store if the page has been visited
- one to store if the reaload has been triggered
在不改变 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';
}
}