3

I had created a dynamic web page. On the main page there are three sliding images. I created another page to change that main page image (sliding). So after I submit that second page, I wrote a PHP code to go back to main page. But the images are not changing because my browser has that web page in its cache. If I remove cache manually, or restart the browser it is working. How I remove my browser cache in PHP coding? give me a solution. Thanks.

sugeesh
  • 506
  • 1
  • 5
  • 21

6 Answers6

5

You may also add a random string to the image-URI:

<img src="myimage.png?r=12345" alt="">

If the random string varies every time the main page is reloaded (!), the browser will retrieve the image from the server.

BurninLeo
  • 4,240
  • 4
  • 39
  • 56
4

There is one trick that can be used.The trick is to append a parameter/string to the file name in the script tag and change it when you file changes.

<script src="myfile.js?version=1.0.0"></script>

The browser interprets the whole string as the file path even though what comes after the "?" are parameters. So wat happens now is that next time when you update your file just change the number in the script tag on your website (Example <script src="myfile.js?version=1.0.1"></script>) and each users browser will see the file has changed and grab a new copy.

Joish
  • 1,458
  • 18
  • 23
3

You can't clear the cache, however you can ask the page not to cache in the first place:

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

This has already been asked on StackOverflow, and this answer was taken from there, purely for the purpose of giving credit to the fellow user who has already answered this question.

The previous question is here: How to clear browser cache with php?

Or, as already said, you can use META tags to do it, but you asked for a PHP solution, so here it is :D

best of luck.

Community
  • 1
  • 1
Chris Evans
  • 1,235
  • 10
  • 14
1

Or you disable your cache with PHP by the header(); function, ore you give a refferance (imgurl.com/imagename.jpg?r=1234) to your image. I would recommend the second method. Why? Because if you shut down the cache for the whole page using the header function op php, it will just slow down your site. So give a random string to your images.

Robin
  • 1,567
  • 3
  • 25
  • 67
1

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>
0

Try using the HTML cache control META TAGS:

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

or use EXPIRES:

<META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 02 May 2015 21:00:00 GMT">