0

I just realized a bug in my code, and I will be having to change a majority of images on my site. But by doing so, a lot of people won't see these NEW images unless cookies are cleared because thats typically what I have to do during the testing process. I was curious if there was a line of code I could place on that one page just to clear what they have so the image refreshes perfectly instead of showing the previous one they are used to.

I can do it in PHP or HTML. Just haven't found anything through google to help yet.

  • I think you mean clear the cache. And if the users see the old images is because you are using the same names, or you have a system that's caching your views. You can't clear the cache of the users but you can use something like: src='path/to/image.jpg?v==rand()?>' This will force the cache to clear for that particular image. If it's really cookies that you need to clear then https://stackoverflow.com/questions/686155/remove-a-cookie – DanielPanic Jun 15 '18 at 16:32
  • How did you code it to make the cookies depend on the viewing of the page? Either way just set a new cookie on top of the old one with 1 second life time and it will delete itself. – Andreas Jun 15 '18 at 16:34
  • Well I store the image path in the database, so whenever I pull it down I do – London O'Connell Jun 15 '18 at 16:38

1 Answers1

1

You can put the below in your php or html -

PHP -

header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

HTML head -

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

And for images - if you are serving the images from Apache web server you can use below htaccess rule -

<IfModule mod_headers.c>
  Header set Cache-Control "no-cache, no-store, must-revalidate"
  Header set Pragma "no-cache"
  Header set Expires 0
</IfModule>
mdeora
  • 4,152
  • 2
  • 19
  • 29