304 未修改问题
抱歉,标题可能有误。我正在编写一些代码来处理 If-Modified-Since 和 If-None-Match 请求作为缓存的一部分。除了 PHP 在标头后面返回一些内容(空行)之外,一切都很完美。页面内容应该为空。我正在使用的代码是:
<?php
$lastmod = filemtime($f);
$etag = '"'.dechex($lastmod).'"';
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('HTTP/1.1 304 Not Modified');
header('Content-Length: 0');
exit();
}
?>
Sorry for the probably wrong title. I am writing some code to handle If-Modified-Since and If-None-Match requests as part of caching. Everything works perfect except for that PHP returns some content (an empty line) after the headers. The page content should be empty instead. The code that I am using is:
<?php
$lastmod = filemtime($f);
$etag = '"'.dechex($lastmod).'"';
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $last_mod || $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
header('HTTP/1.1 304 Not Modified');
header('Content-Length: 0');
exit();
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于解决了这个bug。 Gzip 是罪魁祸首。由于我也在 gzip 压缩对 If-Modified-Since 和 If-None-Match 请求的响应,因此 gzip 向响应添加了一些字节(类似于 gzip 标头)。现在我已经停止对 If-Modified-Since 和 If-None-Match 请求的响应进行 gzip 压缩,它就像一个魅力。
Finally solved this bug. Gzip was the culprit. Since I was gzipping the responses to If-Modified-Since and If-None-Match requests too, gzip was adding a few bytes (kind of a gzip header) to the response. Now I have stopped gzipping responses to If-Modified-Since and If-None-Match requests, and it works like a charm.
试试这个代码:
Try this code:
我在 回答 HTTP_IF_MODIFIED_SINCE 中找到了解决方案PHP 中的 HTTP_IF_NONE_MATCH
创建新文件 caching_headers.php
并将其添加到您要缓存的所有 php 文件中:
I found the solution in Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in PHP
Create new file caching_headers.php
and add this in all php files that you would like to cache: