在网上搜的都是asp的 php怎么阻止后退按钮的缓存呢?
在网上搜的都是asp的 php怎么阻止后退按钮的缓存呢?
无法阻止浏览的history.back()等行为
可以考虑设置页面为无缓存
<code>$nocache = array(
'Expires' => '0',
'Cache-Control' => 'no-store,private, post-check=0, pre-check=0, max-age=0',
'Pragma' => 'no-cache'
);
foreach($nocache as k => $v)
header($k.': '.$v);
</code>如果真的要拒绝后退,可以使用以下方法模拟,考虑做一道跳转,这样后退一次,会自动跳到当前页面,近似于刷新
立即学习“PHP免费学习笔记(深入)”;
<code>header('Location: index.php');</code>
拿首页分页链接来说:<a href="/index.php?page=3" onclick="page(3);return false;">3</a>
搜索引擎的爬虫会根据href访问/index.php?page=3获取第3页数据,利于SEO.
用户在浏览器里右键选择"在新标签页中打开"也能正常访问到/index.php?page=3.
如果用户在页面直接点击链接,则触发click事件,由JS通过AJAX加载并渲染局部数据,以及设置location.hash为/index.php#/page/3.location.hash = "#/page/3";
浏览器能够自行记住location.hash历史记录,我们只需监听location.hash改变的事件hashchange(支持IE8,不支持IE7/6)就能实现用户点击浏览器返回按钮时重新加载页面的效果.
<code>$(window).on("hashchange", function(){
alert(location.hash); //输出#/page/2
var arr = location.hash.split("/"); // ["#", "page", "2"]
if(arr[1] == "page") {
page(arr[2]); //AJAX局部加载第2页数据
//location.href = "/index.php?page="+arr[2]+"&"+new Date().getTime(); //直接访问第2页
}
});</code>上面这种"SEO和体验并重的超链接设计"应该能满足楼主需求,不过需要一定的改造成本.
页面加载完成js刷新页面location.href = location.href
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号