First of all, if you’re using sessions, you must disable session_cache_limiter
(by setting it to none
or public
). Headers it sends are pretty bad for caches.
session_cache_limiter('none');
Then send Cache-Control: max-age=
number_of_seconds
and optionally an equivalent Expires:
header.
header('Cache-control: max-age=".(60*60*24*365));
header("Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365));
For the best cacheability, send Last-Modified
header and reply with status 304 and empty body if the browser sends a matching If-Modified-Since
header.
header('Last-Modified: '.gmdate(DATE_RFC1123,filemtime($path_to_image)));
For brevity I’m cheating here a bit (the example doesn’t verify the date), but it’s valid as long as you don’t mind browsers keeping the cached file forever:
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
header('HTTP/1.1 304 Not Modified');
die();
}