我无法让我的图像脚本在浏览器 (firefox) 中缓存图像。
代码如下:
$type = 'image/jpeg';
$image = '../../files/image.jpeg';
header("Content-type: $type");
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime("1 week")));
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
header('Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],true,304);
exit;
} else {
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($image)).' GMT', true, 200);
echo @readfile($image);
exit;
}
图像被输出,但之后它总是使用 200 响应代码而不是 304 进入 else 语句。我试图强制 304,但看起来图像从未被浏览器缓存。
最佳答案
尝试将其命名为 jpg(在 htaccess 中添加:
RewriteEngine on
RewriteRule image.jpg your_script.php [L]
)
不要忘记更改 your_script.php
和 image.jpg
关于PHP - 浏览器不缓存动态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399343/