PHP 的缓存控制和过期标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6816017/
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
Cache control and expires header for PHP
提问by Brob
I'm setting my headers
我正在设置我的标题
$offset = 60 * 15;
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
header("Cache-Control: max-age=$offset, must-revalidate");
However when running FireBug its giving me the following header information
但是,当运行 FireBug 时,它给了我以下标题信息
HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 12:15:12 GMT
Server: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9
X-Powered-By: PHP/5.2.9
Expires: Sat, 01 Jan 2000 00:00:01 GMT
Cache-Control: post-check=0, pre-check=0, max-age=0
Pragma: no-cache
Last-Modified: Mon, 25 Jul 2011 12:15:13 GMT
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
Anyone know why my headers aren't being recognised?
有谁知道为什么我的标题没有被识别?
回答by JercSi
I know that few versions ago, Firebug had problems and all requests were 200 instead 304. Here is my code, that i'm using for css:
我知道几个版本前,Firebug 有问题,所有请求都是 200 而不是 304。这是我的代码,我用于 css:
<?php
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
$if_modified_since = '';
}
$mtime = filemtime($_SERVER['SCRIPT_FILENAME']);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.0 304 Not Modified");
exit;
}
header("Last-Modified: $gmdate_mod");
header('Content-type: text/css');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (60*60*24*45)) . ' GMT');
// rest of the code
?>
回答by Developer
try this
尝试这个
<META HTTP-EQUIV="Pragma" CONTENT="private">
<META HTTP-EQUIV="Cache-Control" CONTENT="private, max-age=5400, pre-check=5400">
<META HTTP-EQUIV="Expires" CONTENT="<?php echo date(DATE_RFC822,strtotime("1 day")); ?>">
or set the headers in htaccess file. You also need to check your apachi config file for cache configration
或在 htaccess 文件中设置标题。您还需要检查您的 apachi 配置文件以获取缓存配置