Forum Moderators: open

Message Too Old, No Replies

Method to stop browser from caching page

         

nevdev

9:26 pm on Feb 14, 2006 (gmt 0)

10+ Year Member



I'm writing a website which has some pages that display images that are changed quite frequently. That being so, I'm trying to set up a way to prevent the browser from caching these particular pages.

I've tried using PHP to do this, by using a function:

function noCacheHeaders() {
header('Expires: Tue, 1 Jan 1980 12:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, no-transform');
header('Pragma: no-cache');
}

I'm told this should work - but it doesn't.

I also heard of a 'more reliable' method with the .htaccess file, but I only half understand it. I get the impression I should add these lines to the .htaccess file in the directory containing the relevant pages:

<Files mypage.php>
Header append Cache-control "max-age=1"
</Files>

...which is supposed to persuade the caching server to fetch another copy straightaway. But that doesn't work either (and how do I include more than one page?) I always have to manually refresh anyhow.

So how can I stop the pages from being cached?

DrDoc

9:29 pm on Feb 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I am using, and I have not noticed any problems:

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// Always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");