tags:

views:

334

answers:

6

For a small intranet site, I have a dynamic (includes AJAX) page that is being cached incorrectly by Firefox. Is there a way to disable browser caching for a single page?

Here's the setup I'm using:

  • Apache under XAMPP, running on a Windows server
  • PHP

Clarification

The content that I'm primarily concerned about is page text and the default options in some <select>s. So I can't just add random numbers to the end of some image URLs, for example.

Update:

I followed the suggestions I've gotten so far:

  • I'm sending nocache headers (see below)
  • I'm including a timestamp URL parameter and redirecting to a new one if the page is reloaded after 2 seconds, like this:

    $timestamp = $_GET['timestamp']; if ((time()-$timestamp) > 2) { header('Location:/intranet/admin/manage_skus.php?timestamp='.time()); }

Now Firebug shows that the headers specify no cache, but the problem persists. Here are the response headers for the page:

Date    Fri, 25 Sep 2009 20:41:43 GMT
Server  Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8
X-Powered-By    PHP/5.2.8
Expires Mon, 20 Dec 1998 01:00:00 GMT
Last-Modified   Fri, 25 Sep 2009 20:41:43 GMT
Cache-Control   no-cache, must-revalidate
Pragma  no-cache
Keep-Alive  timeout=5, max=100
Connection  Keep-Alive
Transfer-Encoding   chunked
Content-Type    text/html
+6  A: 

Add current timestamp as parameter of url, e.g.

http://server.com/index.php?timestamp=125656789
Anatoliy
This is the best answer since older browsers (i am looking at you IE) don't follow the no-cache header in some situations.
Byron Whitlock
Do this on the link? What about regular page refreshes?
Nathan Long
Redirect browser to new timestamp, if timestamp passed by param is too old.
Anatoliy
+3  A: 

You should send the following header:

Cache-control: no-cache

in the HTTP response.

Martin v. Löwis
I did this, using Craig Treptow's code suggestion, but no dice. I edited the question to show the headers.
Nathan Long
+3  A: 

I think this tells you what you want:

http://www.thesitewizard.com/archive/phptutorial2.shtml

Look for "Preventing the Browser From Caching"

header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
Craig Treptow
Thanks for this. So far no change, though. I edited my question to show the response headers I'm getting.
Nathan Long
+1  A: 

You could add these headers:

Cache-Control: no-cache

And (for backward compatibility with HTTP/1.0 clients)

Pragma: no-cache
Pascal Thivent
A: 

Use the header() function. You have to set a few to cover all browsers; see http://www.php.net/manual/en/function.header.php#75507

Ken Keenan
A: 

Here's another take that isn't PHP specific.

Try this in your <head> </head> section:

<meta http-equiv="cache-control" content="no-cache, no store"/>
<meta http-equiv="Expires" Content="Mon, 25 May 2009 19:07:03 GMT">

Found that at the end of a long thread here:

http://forums.mozillazine.org/viewtopic.php?f=25&amp;t=673135&amp;start=75

Craig Treptow
HTML Meta Tags vs. HTTP Headers: http://www.mnot.net/cache_docs/#META
Pascal Thivent
Great info, thanks Pascal!
Craig Treptow