18

I have a page, index.php, that shows information based on a mysql db. There are forms on it, and the action for the forms is set to a separate page called process.php. Process.php does all the database CRUD stuff, then uses

header("Location: /webadmin/email/index.php");

to send the user back to the original page.

This seems to be working fine, except for the fact that the original index page doesn't always reflect the changes made by process.php. I assume that the page is being cached, because if I do a refresh (Ctrl + F5), the page will show the latest data.

How can I prevent this page from being cached? I have tried what the PHP page for header() says, but it doesn't seem to work. The Cache-Control and Expires options seem to have no effect at all - the page is still being cached.

Update

Ok, I was partially wrong. Apparently, the following does work in IE:

<?php header("Cache-Control: no-cache, must-revalidate");

However, it is definitely NOT working in FF, which is still showing a cached version. Any ideas on why this is, and how I can make it stop caching?

croceldon
  • 4,211
  • 9
  • 51
  • 86
  • 2
    bad news: http://blogs.imeta.co.uk/JDeabill/archive/2008/07/14/303.aspx – danii Dec 15 '09 at 14:20
  • It has apparently been fixed, but i bet there are some ff versions that have experience this behaviour – Peter Lindqvist Dec 15 '09 at 15:03
  • I'm using the very latest version of FF, and I still see this behavior, unless I append a bogus querystring to the end of the URL. – croceldon Dec 15 '09 at 15:10
  • Isn't this a duplicate of http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers ? – YakovL Jun 05 '16 at 21:35

6 Answers6

43

I would play safely and try to output all cache killers known to man (and browser). My list currently consists of:

<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
danii
  • 5,142
  • 2
  • 17
  • 22
  • Do you happen to know the order of precedence of these different headers? I'm sure it varies per-browser, but would be handy to know for at least IE and FF. – AJ. Dec 15 '09 at 14:10
  • 11
    kill it, burn it, hurl the ashes to the sun in a sealed capsule – Stefano Borini Dec 15 '09 at 14:10
  • 1
    I just tried all the above, but it still seems to be caching the page. – croceldon Dec 15 '09 at 14:10
  • @AJ sorry I don't know the order of preference, it depends really in how closely the browser follows the standards. I do know that "pragma" is for HTTP/1.0 and "cache-control" for HTTP/1.1 (i´ll edit the answer) – danii Dec 16 '09 at 07:33
  • Would be nice, would be fine, in a perfect world… but in reality, this just doesn't work. @DerekIllchuk's solution, instead, is ugly, but it actually manages to accomplish the goal. – o0'. May 08 '15 at 10:19
  • This one worked for me.. I had a PHP page built into a WordPress build that was using a caching module, it stopped the module from caching my page – Kender Oct 22 '15 at 15:29
  • @croceldon I don't mean to revive a mummy here but I've learned that the "all-mighty" Apple product Safari (particularly on iPad) will cache the HTML contents regardless of the header information. I would imagine they do this for "user-experience reasons" so that a user can ALWAYS rely on the back button to at minimum see the page which they were previously viewing. – MonkeyZeus Aug 16 '17 at 16:16
17

Make all browsers fall in line:

header("Location: /webadmin/email/index.php?r=".mt_rand(0, 9999999));

It's not pretty, but it fits the question asked: "How to force..."

Derek Illchuk
  • 5,472
  • 1
  • 26
  • 29
15

This is the correct order to get it working on all browsers:

<?php
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // HTTP/1.0
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
?>
moderns
  • 654
  • 8
  • 20
8
<?php header("Cache-Control: no-cache, must-revalidate");
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
0

Try fooling the browser with a spurious querystring:

header("Location: /webadmin/email/index.php?x=1");
graphicdivine
  • 10,539
  • 7
  • 30
  • 58
0
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

do this and it should prevent caching in all browsers

tested in IE FF and chrome

Belldandu
  • 1,660
  • 13
  • 16