4 回答
TA贡献1824条经验 获得超6个赞
首先,后退按钮历史记录不是缓存:
新鲜度模型(第4.2节)不一定适用于历史机制。也就是说,历史机制即使已经过期也可以显示先前的表示。
在旧的HTTP规范中,措辞甚至更强,明确告诉浏览器忽略后退按钮历史记录的缓存指令。
回到应该回到过去(到时候用户在登录)。它不会向前导航到先前打开的URL。
但是,实际上,在非常特定的情况下,缓存可以影响后退按钮:
页面必须通过HTTPS提供,否则此缓存破坏将不可靠。另外,如果您不使用HTTPS,那么您的页面很容易受到许多其他方式的登录窃取。
你必须发送
Cache-Control: no-store, must-revalidate
(一些浏览器观察no-store
和一些观察must-revalidate
)
你永远不需要任何:
<meta>
使用缓存标头 - 它根本不起作用。完全没用。post-check
/pre-check
- 它是仅适用于可缓存资源的IE-only指令。两次或十几个部分发送相同的标题。一些PHP片段实际上取代了以前的标题,导致只发送了最后一个。
如果需要,可以添加:
no-cache
或者max-age=0
,这将使资源(URL)“陈旧”,并要求浏览器检查服务器是否有更新的版本(no-store
已经暗示这更强)。Expires
HTTP / 1.0客户端的过去日期(尽管这些天真正的 HTTP / 1.0仅客户端完全不存在)。
TA贡献1860条经验 获得超8个赞
正如porneL所述,您想要的不是停用缓存,而是停用历史缓冲区。不同的浏览器有自己的微妙方法来禁用历史缓冲区。
在Chrome(v28.0.1500.95 m)中,我们只能这样做Cache-Control: no-store
。
在FireFox(v23.0.1)中,其中任何一个都可以工作:
Cache-Control: no-store
Cache-Control: no-cache
(仅限https)Pragma: no-cache
(仅限https)Vary: *
(仅限https)
在Opera(v12.15)中,我们只能通过Cache-Control: must-revalidate
(仅限https)执行此操作。
在Safari(v5.1.7,7534.57.2)中,任何一个都可以工作:
Cache-Control: no-store
<body onunload="">
在HTML中Cache-Control: no-store
(仅限https)
在IE8(v8.0.6001.18702IC)中,任何一个都可以工作:
Cache-Control: must-revalidate, max-age=0
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: must-revalidate
Expires: 0
Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
(仅限https)Vary: *
(仅限https)
结合上述内容为我们提供了适用于Chrome 28,FireFox 23,IE8,Safari 5.1.7和Opera 12.15的解决方案:( Cache-Control: no-store, must-revalidate
仅限https)
请注意,需要https,因为Opera不会为普通的http页面停用历史记录缓冲区。如果你真的无法获得https并且你准备忽略Opera,你可以做的最好的是:
Cache-Control: no-store<body onunload="">
下面显示了我的测试的原始日志:
HTTP:
Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
失败:Safari 5.1.7,Opera 12.15
成功:Chrome 28,FireFox 23,IE8Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:Safari 5.1.7,Opera 12.15
成功:Chrome 28,FireFox 23,IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: no-store
失败:Safari 5.1.7,Opera 12.15
成功:Chrome 28,FireFox 23,IE8Cache-Control: no-store
<body onunload="">
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: no-cache
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Vary: *
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
成功:无Pragma: no-cache
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
成功:无Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: must-revalidate, max-age=0
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: must-revalidate
Expires: 0
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
失败:Chrome 28,FireFox 23,Safari 5.1.7,Opera 12.15
成功:IE8Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
成功:无
HTTPS:
Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
<body onunload="">
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
成功:无Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
<body onunload="">
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
成功:无Vary: *
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Pragma: no-cache
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: no-cache
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: must-revalidate
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7
成功:Opera 12.15Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0
<body onunload="">
失败:Chrome 28,FireFox 23,IE8,Safari 5.1.7
成功:Opera 12.15Cache-Control: must-revalidate, max-age=0
失败:Chrome 28,FireFox 23,Safari 5.1.7
成功:IE8,Opera 12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,Safari 5.1.7
成功:FireFox 23,IE8,Opera 12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Chrome 28,Safari 5.1.7
成功:FireFox 23,IE8,Opera 12.15Cache-Control: no-store
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:Opera 12.15
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7Cache-Control: private, no-cache
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:Chrome 28,Safari 5.1.7,Opera 12.15
成功:FireFox 23,IE8Cache-Control: must-revalidate
Expires: 0
失败:Chrome 28,FireFox 23,Safari 5.1.7,
成功:IE8,Opera 12.15Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
失败:Chrome 28,FireFox 23,Safari 5.1.7,
成功:IE8,Opera 12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,
成功:IE8,Opera 12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
<body onunload="">
失败:Chrome 28,FireFox 23,Safari 5.1.7,
成功:IE8,Opera 12.15Cache-Control: private, must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:Chrome 28,Safari 5.1.7
成功:FireFox 23,IE8,Opera 12.15Cache-Control: no-store, must-revalidate
失败:无
成功:Chrome 28,FireFox 23,IE8,Safari 5.1.7,Opera 12.15
TA贡献1811条经验 获得超4个赞
我发现web.config路由很有用(尝试将其添加到答案中但似乎没有被接受,因此在此处发布)
<configuration><system.webServer> <httpProtocol> <customHeaders> <add name="Cache-Control" value="no-cache, no-store, must-revalidate" /> <!-- HTTP 1.1. --> <add name="Pragma" value="no-cache" /> <!-- HTTP 1.0. --> <add name="Expires" value="0" /> <!-- Proxies. --> </customHeaders> </httpProtocol></system.webServer>
这是express / node.js做同样的方式:
app.use(function(req, res, next) { res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); res.setHeader('Pragma', 'no-cache'); res.setHeader('Expires', '0'); next();});
添加回答
举报