You will need to do a few things.
Firstly, you need to set your expires and cache-control headers. Depending on the browsers you want to support, you can skip the Expires header. You will probably want to take a read of the HTTP spec to understand all of the options available in Cache-Control
Code:
$max_age = 60 * 60 * 24; // 24 hours
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
header('Cache-Control: cache, store, max-age=' . $expires . ', must-revalidate');
Secondly, you will need to support one or both of ETag/If-None-Match or Last-Modified/If-Modified-Since. These are headers which allow for cache revalidation. You don't need to support these, but you need to support them if you want to be able to expire early. If you're not supporting these, ditch the must-revalidate clause in Cache-Control.
Here is a function I use to set (and check) the etag, as well as setting the correct headers:
Code:
function etag($etag, $max_age) {
header('ETag: "' . $etag . '"');
header('Pragma: public');
header('Cache-Control: store, cache, must-revalidate, max-age=' . $max_age);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
if ($_SERVER['HTTP_IF_NONE_MATCH']) {
$match = str_replace('"', '', $_SERVER['HTTP_IF_NONE_MATCH']);
if ($match == $etag) {
header('HTTP/1.0 304 Not Modified');
exit;
}
}
}
You then call it like this:
Code:
$etag = md5($something_unique);
$max_age = 60 * 60 * 24;
etag($etag, $max_age);
echo 'This should be cached for 24 hours';
The variable $something_unique needs to be unique for each version of the page. If the page content changes, this variable needs to change too (or the etag won't work). Likewise, if the content doesn't change, the etag should remain the same. If you were doing a revision-based cms, you could use the page id and revision id as the unique key. When a new revision goes live, the revision id would change, and the etag would likewise change.
Don't forget to take a look at RedBot (
http://redbot.org/) which will help a lot.
_________________
Chaotic Rage - a fast paced, shooter game that's a little odd, but still fun.