This little piece of code checks the last modification time of the script it's
in, generates an ETag based on the full REQUEST_URI + timestamp, and checks to
see if it can send back a 304 Not Modified status instead of continuing on
processing the request further on.
For older browsers, it will also send a Last-Modified header, and check for a
If-Modified-Since header in the request to achieve the same thing...
Note that there are some curious side-effects - for instance, if you're viewing
a page as anonymous visitor and you log in, the (cached) page you're on will
still show you as *not* logged in, while any other part of the site will see
that you *are* logged in :-)
$thisfile = $SCRIPT_FILENAME;
if (!empty($thisfile) && file_exists($thisfile)) {
$thistime = filemtime($thisfile);
$md5 = md5($REQUEST_URI . $thistime);
header("ETag: $md5");
global $HTTP_SERVER_VARS;
if (!empty($HTTP_SERVER_VARS['HTTP_IF_NONE_MATCH']) &&
preg_match("/$md5/", $HTTP_SERVER_VARS['HTTP_IF_NONE_MATCH'])) {
header('HTTP/1.0 304');
// For IIS ?
//header('Status: 304 Not Modified');
exit;
} elseif (!empty($HTTP_SERVER_VARS['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($HTTP_SERVER_VARS['HTTP_IF_MODIFIED_SINCE']) >= $thistime) {
header('HTTP/1.0 304');
// For IIS ?
//header('Status: 304 Not Modified');
exit;
}
header("Last-Modified: " . gmdate("D, d M Y H:i:s",$thistime) . " GMT");
session_cache_limiter('private');
// For Mozilla ?
//session_cache_limiter('private_no_expire');
}
For more tips and tricks, see
Mike's Pub