PHPで`If-Modified-Since`をチェックする
2006年3月1日
"HTTP/1.1 304 Not Modified"で返す際の判定ルーチン。NP_MediaFiles用のthumbnail.phpで使用しようと製作したが、速度の向上が認められず削除。製作したルーチンは簡潔で、しかもうまく働いていただけに、捨てるには忍びずここにメモすることにした。
<?php
(略)
$request_headers = apache_request_headers();
if($string_date=$request_headers["If-Modified-Since"]) {
$lastmodified=array( gmstrftime("%a, %d %b %Y %H:%M:%S GMT", filemtime($imageFile)),
gmstrftime("%A, %d-%b-%y %H:%M:%S GMT", filemtime($imageFile)),
preg_replace('/ 0([0-9]) /',' $1 ',gmstrftime("%a %b %d %H:%M:%S %Y", filemtime($imageFile))));
foreach ($lastmodified as $value) {
if (strtolower($value)!=strtolower($string_date)) continue;
header ("HTTP/1.1 304 Not Modified");
header ("Date: ".$lastmodified[0]);
exit;
}
}
(略)
header ("Content-Type: image/$type");
header ("Content-Length: ".strlen($buff));
header ("Last-Modified: ".gmstrftime("%a, %d %b %Y %H:%M:%S GMT", filemtime($imageFile)));
echo $buff;
?>