如何强制页面不被缓存在 PHP 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1907653/
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
How to force page not to be cached in PHP?
提问by croceldon
I have a page, index.php, that shows information based on a mysql db. There are forms on it, and the action for the forms is set to a separate page called process.php. Process.php does all the database CRUD stuff, then uses
我有一个页面 index.php,它显示基于 mysql 数据库的信息。上面有表单,表单的操作设置在一个名为 process.php 的单独页面中。Process.php 完成所有数据库 CRUD 的工作,然后使用
header("Location: /webadmin/email/index.php");
to send the user back to the original page.
将用户发送回原始页面。
This seems to be working fine, except for the fact that the original index page doesn't always reflect the changes made by process.php. I assume that the page is being cached, because if I do a refresh (Ctrl + F5), the page will show the latest data.
这似乎工作正常,除了原始索引页并不总是反映 process.php 所做的更改。我假设页面正在被缓存,因为如果我刷新(Ctrl + F5),页面将显示最新数据。
How can I prevent this page from being cached? I have tried what the PHP page for header() says, but it doesn't seem to work. The Cache-Control and Expires options seem to have no effect at all - the page is still being cached.
如何防止此页面被缓存?我已经尝试过header() 的 PHP 页面所说的内容,但它似乎不起作用。Cache-Control 和 Expires 选项似乎根本没有效果 - 页面仍在缓存中。
Update
更新
Ok, I was partially wrong. Apparently, the following does work in IE:
好吧,我错了。显然,以下在 IE 中确实有效:
<?php header("Cache-Control: no-cache, must-revalidate");
However, it is definitely NOT working in FF, which is still showing a cached version. Any ideas on why this is, and how I can make it stop caching?
但是,它绝对不能在 FF 中工作,它仍然显示缓存版本。关于为什么会这样,以及如何让它停止缓存的任何想法?
采纳答案by Derek Illchuk
Make allbrowsers fall in line:
使所有浏览器都符合要求:
header("Location: /webadmin/email/index.php?r=".mt_rand(0, 9999999));
It's not pretty, but it fits the question asked: "How to force..."
它不漂亮,但它符合提出的问题:“如何强制......”
回答by danii
I would play safely and try to output all cache killers known to man (and browser). My list currently consists of:
我会安全地玩游戏并尝试输出人类(和浏览器)已知的所有缓存杀手。我的清单目前包括:
<?php
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: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
回答by moderns
This is the correct order to get it working on all browsers:
这是让它在所有浏览器上工作的正确顺序:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // HTTP/1.0
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
?>
回答by Sarfraz
<?php header("Cache-Control: no-cache, must-revalidate");
回答by graphicdivine
Try fooling the browser with a spurious querystring:
尝试用虚假的查询字符串欺骗浏览器:
header("Location: /webadmin/email/index.php?x=1");
回答by Belldandu
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
do this and it shouldprevent caching in all browsers
这样做,它应该可以防止在所有浏览器中缓存
tested in IE FF and chrome
在 IE FF 和 chrome 中测试

