我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
当前回答
我用这种方式解决了。
2个注意事项:
1) 服务器端事件不是在后退单击时触发的,而是javascript。
2) 我有两个javascript来读/写cookie
function setCookie(name, value, days)
{
var expires = "";
if (days)
{
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = ca.length - 1; i >= 0; i--)
{
var c = ca[i];
while (c.charAt(0) == ' ')
{
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0)
{
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
在我的Page_Load中,我插入了以下内容:(这不是在单击后启动的)
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("", "<script>setCookie('" + Session.SessionID + "', '" + Login + "', '100');</script>");
}
其中“Login”是我的id值,注销后为-1(可以使用其他值,例如布尔值)。
然后在我的页面中添加了以下内容:(这是在单击后启动的)
<script type="text/javascript">
if (getCookie('<%= Session.SessionID %>') < 0)
{
if (history.length > 0)
{
history.go(+1);
}
}
</script>
没有别的。
使用此解决方案,仅在同一浏览器上的每个页面注销后,才在每个页面上启用反向单击。
其他回答
我发现这一页上的所有答案仍然存在问题。特别是,我注意到,当您通过单击后退按钮访问页面时,它们都不会阻止IE8使用缓存版本的页面。
经过大量研究和测试,我发现我真正需要的两个标题是:
缓存控制:无存储变化:*
有关Vary标头的说明,请查看http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.6
在IE6-8、FF1.5-3.5、Chrome 2-3、Safari 4和Opera 9-10上,当您单击页面链接或将URL直接放入地址栏时,这些标题会导致服务器请求页面。截至2010年1月,这涵盖了99%的浏览器。
在IE6和Opera 9-10上,点击后退按钮仍然会加载缓存版本。在我测试的所有其他浏览器上,它们确实从服务器获取了新版本。到目前为止,我还没有找到任何一组标头,当您单击后退按钮时,这些标头会导致这些浏览器无法返回缓存的页面版本。
更新:写下这个答案后,我意识到我们的web服务器正在将自己标识为HTTP 1.0服务器。我列出的标头是正确的,以便浏览器不会缓存来自HTTP 1.0服务器的响应。对于HTTP 1.1服务器,请查看BalusC的答案。
此外,如果您使用.htaccess文件启用缓存,请确保重置该文件中的ExpiresDefault。
ExpiresDefault "access plus 0 seconds"
然后,可以使用ExpiresByType为要缓存的文件设置特定值:
ExpiresByType image/x-icon "access plus 3 month"
如果您的动态文件(如php等)被浏览器缓存,并且您无法找出原因,那么这可能也很有用。选中ExpiresDefault。
头函数的PHP文档有一个相当完整的示例(由第三方提供):
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false); // HTTP/1.1
header ("Pragma: no-cache");
header("Expires: 0", false);
我在<head><meta>元素方面运气不佳。直接(在HTML文档之外)添加与HTTP缓存相关的参数确实对我有用。
Python中使用web.py-web.header调用的示例代码如下。我有目的地编辑了我个人无关的实用程序代码。
import web import sys import PERSONAL-UTILITIES myname = "main.py" urls = ( '/', 'main_class' ) main = web.application(urls, globals()) render = web.template.render("templates/", base="layout", cache=False) class main_class(object): def GET(self): web.header("Cache-control","no-cache, no-store, must-revalidate") web.header("Pragma", "no-cache") web.header("Expires", "0") return render.main_form() def POST(self): msg = "POSTed:" form = web.input(function = None) web.header("Cache-control","no-cache, no-store, must-revalidate") web.header("Pragma", "no-cache") web.header("Expires", "0") return render.index_laid_out(greeting = msg + form.function) if __name__ == "__main__": nargs = len(sys.argv) # Ensure that there are enough arguments after python program name if nargs != 2: LOG-AND-DIE("%s: Command line error, nargs=%s, should be 2", myname, nargs) # Make sure that the TCP port number is numeric try: tcp_port = int(sys.argv[1]) except Exception as e: LOG-AND-DIE ("%s: tcp_port = int(%s) failed (not an integer)", myname, sys.argv[1]) # All is well! JUST-LOG("%s: Running on port %d", myname, tcp_port) web.httpserver.runsimple(main.wsgifunc(), ("localhost", tcp_port)) main.run()
如果您面临通过SSL和cache:MS Office文件的IE6-IE8下载问题,则可以在POST请求时使用cache:private、no store header和return file。它起作用了。