3

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', true, 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.

Alex
  • 31
  • 2
  • Try this answer: http://stackoverflow.com/questions/2970938/ideal-http-cache-control-headers-for-different-types-of-resources/3001556#3001556 – OZ_ Jun 18 '11 at 22:51
  • 1
    Then that would mean `HTTP_IF_MODIFIED_SINCE` is not set by the server. – Jason McCreary Jun 18 '11 at 22:51

1 Answers1

0

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

genesis
  • 50,477
  • 20
  • 96
  • 125