How to clear browser cache with php?
4 Answers
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");

- 10,107
- 10
- 37
- 39
-
4Clearing the cache everytime isn't always the best option though. It might be worth reading up on. – Jon Winstanley Jun 24 '09 at 10:29
-
6
-
31this doesn't clear the user's browser cache. It does tell the browser to not cache this particular page, however. – DA. Jun 15 '12 at 20:30
-
2A good way for not having to teach your client how to clear cache. – Glorious Kale May 14 '13 at 09:28
-
1this code is not working for me sir... i have to reload manually my page. – always-a-learner Jan 02 '16 at 10:18
-
This will prevent future caching of the affected resource, but till not clear existing caches. – pschichtel Jun 26 '18 at 12:01
You can delete the browser cache by setting these headers:
<?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");
?>

- 1,402
- 1
- 15
- 21

- 502
- 5
- 12
-
This works for me. I tried to make a preview of a 6MB pdf. My script working for small sized pdfs but not more than 3 mbs. – user2439124 May 29 '18 at 11:06
-
This is the way how to clear cache including loaded audio or image file path. – Rowf Abd Aug 20 '18 at 20:14
-
3This won't clear a cache that's already there. If the browser has visited the page previously and the headers were set to cache, it will most likely just load the cached files and never receive the new no-cache headers until the user manually refreshes the content on their end. – Peter Cullen Dec 13 '18 at 01:07
With recent browser support of "Clear-Site-Data" headers, you can clear different types of data: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data
header('Clear-Site-Data: "cache", "cookies", "storage", "executionContexts"');

- 456
- 7
- 9
It seams you need to versionate, so when some change happens browser will catch something new and user won't need to clear browser's cache.
You can do it by subfolders (example /css/v1/style.css)
or by filename (example: css/style_v1.css)
or even by setting different folders for your website, example:
www.mywebsite.com/site1
www.mywebsite.com/site2
www.mywebsite.com/site3
And use a .htaccess or even change httpd.conf to redirect to your current application.
If's about one image or page:
<?$time = date("H:i:s");?>
<img src="myfile.jpg?time=<?$time;?>">
You can use $time on parts when you don't wanna cache. So it will always pull a new image. Versionate it seams a better approach, otherwise it can overload your server. Remember, browser's cache it's not only good to user experience, but also for your server.

- 1,668
- 2
- 20
- 25