禁止浏览器缓存- make sure web page is not cached

来源:互联网 发布:网络评选投票 编辑:程序博客网 时间:2024/06/08 06:49

如何禁止浏览器缓存,网上搜到的解决方法都测试无效。

基本上全都是

Cache-Control: no-cachePragma: no-cacheExpires: 0

Google了一下,找到了解决方法。

设置response header 的效果就是 返回的时候一定是重新请求页面的

Using PHP:

header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.header("Pragma: no-cache"); // HTTP 1.0.header("Expires: 0"); // Proxies.

Using Java Servlet, or Node.js:

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.response.setHeader("Pragma", "no-cache"); // HTTP 1.0.response.setHeader("Expires", "0"); // Proxies.

Using ASP.NET:

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.Response.AppendHeader("Expires", "0"); // Proxies.

Using ASP:

Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.Response.addHeader "Expires", "0" ' Proxies.

Using Ruby on Rails, or Python on Flask:

response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.response.headers["Pragma"] = "no-cache" # HTTP 1.0.response.headers["Expires"] = "0" # Proxies.

Using Google Go:

responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.responseWriter.Header().Set("Expires", "0") // Proxies.

Using Apache .htaccess file:

<IfModule mod_headers.c>    Header set Cache-Control "no-cache, no-store, must-revalidate"    Header set Pragma "no-cache"    Header set Expires 0</IfModule>

Using HTML4:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /><meta http-equiv="Pragma" content="no-cache" /><meta http-equiv="Expires" content="0" />

HTML meta tags vs HTTP response headers

根据SO上面的说法,

如果通过HTTP访问,HTTP response headers 是优先于 meta tags 的。但

是,第一次打开是通过HTTP访问的,而返回的时候是从本地读取的。


我在自己尝试的时候,发现这两个都需要设置 才能清除页面表单记录。Google浏览器 和 IE11测试通过,页面的记录消除。其他的浏览器未测试。(推测是因为上面的原因)
如果多次测试发现 页面表单的记录还在。

但是可以保证,只要写了HTTP response headers 返回的时候一定会重新请求。

部分代码如下
<%@ page language="java" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.response.setHeader("Pragma", "no-cache"); // HTTP 1.0.response.setHeader("Expires", "0"); // Proxies.%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>index.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" ><meta http-equiv="expires" content="0" >

传送门:StackOverFlow


应用

在防止表单重复提交的时候非常有用。
2 0
原创粉丝点击