How to Clear Browser Cache With PHP

How to clear browser cache with php?


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

can we forcibly clear browser cache through some code

Firstly, this depends on how long you've sent the expires header for -- a day? Week? Month?

No matter when you've set the expires header, you have to wait until that time is up before the browser even begins to contact the server for a new version. So you will have to change the url as others have pointed out.

However, there is another good option for you, for the future...

If you set 'cache-control: "no-cache, must-revalidate"' and a short Expires time, then the browser will check with the server every time it wants to re-display a cached object, using an "If-Modified-Since" request header to send the last timestamp that your server originally sent with that cached object in its "Last-Modified" response header. If the object has been updated since the timestamp sent by the client, then the server will send the new object and a new Last-Modified timestamp header. If not, it will respond with only a "304-Not Modified" response header.

So, the advantage of revalidation is that you still save some bandwidth with little risk of the client showing "stale" objects, but the disadvantage is that the client must wait while your server checks the client's If-Modified-Since header against the file's "Last-Modified" timestamp and of course, the server has to actually go check the filesystem to get that "Last-Modified" time. So all that's saved is the actual content transfer bandwidth and transfer time.


Good reading resources:

  • http://www.mnot.net/cache_docs/
  • http://code.google.com/speed/page-speed/docs/caching.html


Related Topics



Leave a reply



Submit