11 Vote

Clear browser cache using PHP?

Question by Guest | 2012-08-19 at 20:23

I want to delete the browser's cache of my visitors with the help of PHP. Some files that were valid for a while, have now been replaced by me and I therefore, I would like to achieve, that those files cannot be loaded from the cache of my visitors any longer.

Is there any PHP script that you can recommend?

ReplyPositiveNegativeDateVotes
24 Votes

I have to disappoint you in your project, unfortunately. With PHP, you can not affect the browser cache of your visitors.

Once stored on the computer, unfortunately, you have never again access to this data. If the browser would let you do something like that, you could just delete the whole hard drive of your visitors super easy!

Only if you are using plugins like ActiveX or likewise, you can do such things, but everyone has disabled those plugins and it also makes not a good impression, if someone is surfing on your website and the virus scanner says, that the page would like to delete some files from your hard drive.
2012-08-20 at 15:55

ReplyPositive Negative
00 Votes

No true. Still works.

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

ISPs can bypass though. But the browser rules prevents browsers from caching the page and would call a whole fresh page again when access is made. Sometimes important for dynamic website applications and still used.
2018-09-22 at 11:54

Positive Negative
Reply
55 Votes

If your files are often changing, in your place, I would simply send the following header with PHP along with them:

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

You can add these lines just before the first issue of your PHP script into your pages. With this, you tell the browser, that it should not cache the page and it should request the contents every time again.

For pictures, you can write the following line into your HTACCESS:

<FilesMatch "\.(jpg|png|gif)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>

What files and which file types should be affected closely, you can of course customize individually. So you can, for example, create a folder for all the images that are changing often and another folder for all the other images, that do not and could be cached without problems.
2012-08-21 at 19:16

ReplyPositive Negative
26 Votes

Here's a small addition on this subject from me:

Reload pictures and websites despite cache
Last update on 2023-01-13 | Created on 2012-08-23

ReplyPositive Negative
Reply

Related Topics

PHP: File Download Script

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Upload of large Files

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.