How to update image cache via PHP

0

I have a thumbnail image in header.php , but when sending a request to update that image to another page in PHP (which updates the same in the database), it is not updated automatically on the page.

Obs. : I have already inserted the following headers in header.php, without success:

header("Pragma: no-cache");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, cachehack=".time());
header("Cache-Control: no-store, must-revalidate");
header("Cache-Control: post-check=-1, pre-check=-1", false);
    
asked by anonymous 23.07.2017 / 16:41

1 answer

1

The most guaranteed way is to add a new parameter to the image link. Example: imagem.jpg?20170724 . If you modify tomorrow it would look something like imagem.jpg?20170725 .

You can try all these tricks with header() but it will not always work in all browsers as you can see. In 18 years dealing with this, I tried all these header pragma , h eader expires and header macumba vela preta , and when I thought I'd found the right combination, a browser update would turn everything off. I simply gave up and opted for the simplest and obvious that is to add the file version as a parameter or the last modified date and time.

In the database, create a field "date_modified" and whenever you search the image, also read this field to concatenate it in the URL.

Another technique used is to generate a file with a new unique name. So you would not have to worry about putting the date or version as a parameter of the URL. The disadvantage of this is if the image is used for SEO because the previous one is given as Not found by the search engines and the site loses rankings in the search for images (google images, for example). >

Note that the parameterized technique does not update the existing cache. What happens is that the cache remains and the new file is downloaded because to the browser, 1 different parameter is given as new file.

    
23.07.2017 / 20:55