jd.*_*jd. 14 php caching http http-headers
我正在使用PHP脚本来提供文件.我希望能够304在我的http响应中发送一个未修改的标头,如果该文件自客户端上次下载以来没有更改.这似乎是Apache(和大多数其他Web服务器)的一个功能,但我不知道如何通过PHP实现它.
我听说过使用过$_SERVER['HTTP_IF_MODIFIED_SINCE'],但是这个变量似乎没有出现在我的$_SERVER超级数组中.
我的问题不是如何返回304标题,而是如何知道应该返回标题.
编辑:问题是我$_SERVER['HTTP_IF_MODIFIED_SINCE']没有设置.这是我.htaccess文件的内容:
ExpiresActive On
ExpiresByType image/jpeg "modification plus 1 month"
ExpiresByType image/png "modification plus 1 month"
ExpiresByType image/gif "modification plus 1 month"
Header append Cache-Control: "must-revalidate"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(controller\.php)
RewriteRule (.*\.jpg|.*\.png|.*\.gif) controller.php/$1
</IfModule>
Run Code Online (Sandbox Code Playgroud)
HTTP_IF_MODIFIED_SINCE仍然没有出现在$_SERVER超级阵列中.
小智 26
HTTP_IF_MODIFIED_SINCE是正确的方法.如果你没有得到它,检查阿帕奇已经mod_expires和mod_headers启用并正常运行.借用PHP.net上的评论:
$last_modified_time = filemtime($file);
$etag = md5_file($file);
// always send headers
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("Etag: $etag");
// exit if not modified
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
header("HTTP/1.1 304 Not Modified");
exit;
}
// output data
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17355 次 |
| 最近记录: |