James.Cridland.net

No caching EVER

Defeating a browser's cache, or a proxy's cache, is more than using META tags. AOL ignores them, for a start, and anyway, you're better using proper headers. Thankfully, PHP lets you do that. So, if you NEVER want to cache a page, use this, right at the top of the page. I mean right at the top - no blank spaces, nothing.

<?
//Set no caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

You probably don't want to ever use that, being honest: most of the time, you're better to only stop caching when you want your user to see a new version of a page they've just seen. So, if you only want to not-cache a page in certain circumstances, like returning to a forum index after you've allowed the user to add a new message, it's easier to fix this with your links...

<?
//Link to a non-cacheable version of a page
echo "<a href='index.html?nocache='.time().'>Go back</a>";
?>

This code will give you a link like caching.html?nocache=1215317908 - that number at the end changes once every second, so any proxy will have to reload it. Remember, though, that the page you put this link on may also cache.