user950276
user950276

Reputation: 49

How to Clear Cache using PHP

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

Upvotes: 1

Views: 16908

Answers (4)

user1032289
user1032289

Reputation: 502

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");
?>

Upvotes: 1

deizel.
deizel.

Reputation: 11212

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
}

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88647

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.

Upvotes: 5

Nahydrin
Nahydrin

Reputation: 13507

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.

Upvotes: 2

Related Questions