2

The following code is what I am using in my image.php (image.jpeg too under htaccess). I want the user to be able to access a cached copy if the source hasn't been changed (thus it is the same image). However if the source has been changed get the new copy. This image is a background image thus caching is important. Is this code correct? I tried it in Chrome and refreshing the page always reloaded the image. Going to the page again (clicking enter) always kept the cached image even when it had been updated.

<?php
session_start(); 
header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
if(file_exists('settings.xml')){
        $xml = simplexml_load_file('settings.xml');
        define("BACKGROUND_IMAGE", $xml->background->image);
        define("BACKGROUND_TIME", $xml->background->time); // when image changes this is set to time()
    }
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) 
       && 
  (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == BACKGROUND_TIME)) {
  // send the last mod time of the file back
  header('Last-Modified: '.gmdate('D, d M Y H:i:s', BACKGROUND_TIME).' GMT', 
  true, 304);
  exit;
}
// open the file in a binary mode
$name = BACKGROUND_IMAGE;
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
michaellindahl
  • 2,012
  • 5
  • 36
  • 64
  • refreshing (F5) always reloads images. You can achieve your goal by using JS (set bg-pic after page is loaded). – el Dude Feb 02 '13 at 12:02

1 Answers1

2

Personally I use something like this and works perfectly;

$etag = '"'. md5($img) .'"';
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) 
       && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
    header('HTTP/1.1 304 Not Modified');
    header('Content-Length: 0');
    exit;
}

$expiry = 604800; // (60*60*24*7)
header('ETag: '. $etag);
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires:'.        gmdate('D, d M Y H:i:s', time() + $expiry) .' GMT');
...
// show/send/read image

But here is something else if you like to see (ref: Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in PHP).

Community
  • 1
  • 1
Kerem
  • 11,377
  • 5
  • 59
  • 58
  • The [top answer in the link you provided](http://stackoverflow.com/a/2015665/482115) works flawlessly. Thank you! – degenerate Feb 10 '15 at 18:31