[英]Header Cache-Control do not work header “Last-Modified”
I make an Image with php and would like to control the cache-time. 我用php制作图像,并希望控制缓存时间。 I have this code:
我有以下代码:
header("Cache-Control: must-revalidate");
$fn = gmdate('D, d M Y H:i:s \G\M\T', time() + 60);
$now = gmdate('D, d M Y H:i:s \G\M\T', time());
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) <= $now )
{
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: '.$fn, true, 304);
}else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: '.$fn, true, 200);
//Header
header("Content-type: image/PNG");
//Ausgeben
imagePNG($bild);
};
It should give a new image only after 60sec. 它应该仅在60秒后给出新图像。 But my code gives it always.
但是我的代码总是给它。
I think something with your arithmetic is off; 我认为您的运算法则已失效; Look at the following example based on your code:
根据您的代码查看以下示例:
$lifetime = 60;
header("Cache-Control: must-revalidate");
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$lastMod = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
} else {
$lastMod = 0;
}
if ($lastMod <= $_SERVER['REQUEST_TIME'] - $lifetime) {
// Time to refresh
$lastMod = $_SERVER['REQUEST_TIME'];
header("Content-type: text/plain");
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $lastMod), true, 200);
echo "Hello!";
} else {
header("Last-Modified: " . gmdate('D, d M Y H:i:s \G\M\T', $lastMod), true, 304);
}
This will set the last-modified header to now (using $_SERVER['REQUEST_TIME']
which is likely much more efficient for your need than using time()
directly), and on subsequent requests check if If-Modified-Since is at least 60 seconds old. 这会将最后修改的标头设置为现在(使用
$_SERVER['REQUEST_TIME']
可能比直接使用time()
更高效),并在后续请求中检查If-Modified-Since是否至少60秒大。 If so, it will refresh (and re-set last-modified to now); 如果是这样,它将刷新(并将上一次修改的值重置为现在); otherwise, 304 is returned and last-modified is not changed.
否则,返回304,并且最后修改不变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.