php 中 http 标头的缓存问题
我正在开发一个显示图像的 php 服务。我的问题是我无法正确处理浏览器缓存。
这是代码:
date_default_timezone_set('Europe/Brussel');
$expireTime = 7200;
header('Date: '.gmdate('D, d M Y H:i:s', time()).' GMT');
header('Expires: '.gmdate('D, d M Y H:i:s', time() + $expireTime).' GMT');
header('Cache-Control: max-age='. ($expireTime).', public'); // must-revalidate ?
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($path)).' GMT');
header('Content-Type: '.HelpersFile::getMIME($path));
header('Content-Length: ' . filesize($path));
header('Content-Disposition: inline;');
header('Pragma: public');
$fileRessource = fopen($path, 'rb');
fpassthru($fileRessource);
header('Connection: Close');
exit();
http://web-sniffer.net/ 中的结果:
状态:HTTP/1.1 200 正常 日期:2012 年 1 月 16 日星期一 21:21:25 GMT
服务器:Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze3
设置 Cookie:PHPSESSID=86m4rha4rvth4q8srrpg48t4t0;路径=/
到期时间:2012 年 1 月 16 日星期一 23:21:25 GMT
缓存控制:max-age=7200,公共
编译指示:公共
最后修改时间:2012 年 1 月 15 日星期日 14:42:39 GMT
内容长度:223247
内容配置:内联; 连接:关闭
内容类型:图像/png
最后,您可以在这里看到 PHP 服务: http://spinephp.info/service/MediaLibrary/mediaWithId/1.jpg
如果我将文件加载到 Chrome 或任何其他浏览器中,我总是会从服务器收到 200 OK 回复,并且浏览器永远不会从缓存中加载文件。
我不知道我做错了什么。 有人对我有线索吗?
[编辑] 更准确地说,我想要的是客户端浏览器在2个小时内根本不查询服务器。这可能吗?
I work on a php service that displays images. My problem is that I can not handle the browser cache properly.
This is the code:
date_default_timezone_set('Europe/Brussel');
$expireTime = 7200;
header('Date: '.gmdate('D, d M Y H:i:s', time()).' GMT');
header('Expires: '.gmdate('D, d M Y H:i:s', time() + $expireTime).' GMT');
header('Cache-Control: max-age='. ($expireTime).', public'); // must-revalidate ?
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($path)).' GMT');
header('Content-Type: '.HelpersFile::getMIME($path));
header('Content-Length: ' . filesize($path));
header('Content-Disposition: inline;');
header('Pragma: public');
$fileRessource = fopen($path, 'rb');
fpassthru($fileRessource);
header('Connection: Close');
exit();
And this the result in http://web-sniffer.net/:
Status: HTTP/1.1 200 OK
Date: Mon, 16 Jan 2012 21:21:25 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze3
Set-Cookie: PHPSESSID=86m4rha4rvth4q8srrpg48t4t0; path=/
Expires: Mon, 16 Jan 2012 23:21:25 GMT
Cache-Control: max-age=7200, public
Pragma: public
Last-Modified: Sun, 15 Jan 2012 14:42:39 GMT
Content-Length: 223247
Content-Disposition: inline;
Connection: close
Content-Type: image/png
And finaly, you can see the PHP service here:
http://spinephp.info/service/MediaLibrary/mediaWithId/1.jpg
If I load the file into Chrome or any other browser, I always get an 200 OK reply from the server and the browser never load the file from it's cache.
I've no idea what I'm doing wrong.
Does anyone have a clue for me?
[Edit]
To be more precise, what I want is that the client browser does not query the server at all during 2 hours. Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要评估客户端发送的
If-Modified-Since
标头。如果此后文件没有被修改,您应该只发回304
响应。您可以通过执行
301
重定向到实际文件来节省一些代码,并让 Web 服务器完成这项工作。You need to evaluate the
If-Modified-Since
header sent by the client. If the file hasn't been modified since then, you should send only a304
response back.You might be able to save yourself some code by doing a
301
redirect to the actual file and let the web server do the work.它对我有用。只是不要按 F5,因为这会强制刷新。要测试它,只需单击地址栏并按 Enter 键即可。
如果您确实进行了真正的刷新,则解析 If-Modified-Since 是一个额外的好处。
It's working for me. Just don't hit F5, because that will force a refresh. To test it, simply click your addressbar and hit enter.
Parsing If-Modified-Since is an extra benefit, if you do do a real refresh.