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;