1

i am trying to clear browser Cache using PHP

here is my code

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: application/xml; charset=utf-8");

this code in not working can anybody have idea .

thanks in advance

user950276
  • 49
  • 1
  • 1
  • 7
  • 3
    Just curious why so much `header` examples use `July 26th`, my birthday. What is so special in that date (except the fact that I was born)? – zerkms Oct 06 '11 at 11:56
  • 2
    @zerkms: We celebrate zmayte's arrival on this earth internationally :P – alex Oct 06 '11 at 11:59
  • 2
    @zerkms It's just because it's your birthday. Has to be it. http://en.wikipedia.org/wiki/July_26 – DaveRandom Oct 06 '11 at 12:01

4 Answers4

5

You can't clear the cache from the server side, only instruct the browser not to do any more caching.

The headers you have used will work - they will tell the browser not to cache the content you just sent. However, if the browser already has a cached version of the page, it won't send a request, and will not get the headers you are setting, so it won't know to abandon the cached version.

Press CTRL+F5 to force the browser to refresh the content. After you do this, you should get the expected behaviour.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
2

You cannot clear the local browser cache using PHP. You can only clear sessions/cookies that the user has on the website running the PHP script.

Nahydrin
  • 13,197
  • 12
  • 59
  • 101
1

Please add this code to your php page

<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
user1032289
  • 502
  • 5
  • 12
1

We use this in production to prevent users from being able to view authenticated pages after they have logged out by pressing back on their browser (it lives in AppController::beforeFilter()):

// disable local browser cache for authenticated pages (so user can't press back after logging out)
if ($this->Auth->user()) {
    $this->header("Cache-Control: no-cache, no-store, must-revalidate"); // 'no-store' is the key
    $this->header("Expires: Mon, 1 Jan 2001 00:00:00 GMT"); // date in the past
}
deizel.
  • 11,042
  • 1
  • 39
  • 50