开发者

PHP - browser not caching dynamic image

I am having trouble getting my image script to cache the image in the browser (firefox).

Here is the code :

$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', tru开发者_开发技巧e, 200);
    echo @readfile($image);
    exit;
    }

The image gets outputted, however after that it always goes to the else statement with the 200 response code instead of the 304. I tried to force the 304, but it looks like the image is never cached by the browser.


Try to call it as jpg (add this in htaccess:

RewriteEngine on
RewriteRule image.jpg your_script.php [L]

)

don't forgot to change your_script.php and image.jpg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜