0

There is a blog where I can post images and links, no scripts. I still want to have dynamic content.

My idea: image that has no-cache etc. headers and is dynamically generated by my own server-side php script. This image links to a page on my server, that sets cookie and does javascript history.back(). Then next time the image is downloaded, it is a different image.

Problem is, I can't get browsers to download the image again after back button is pressed...

I have set following headers:

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

Still for some reason if the image is inside a tag, the old version is loaded from cache when going back.

Also I included the image on the seccond page, so browser did even download the new version of the image, but when i pressed the back button, I was still presented with the old version...

How can I force browser to download the image again?

a966821
  • 1
  • 1
  • You are working on desktop browsers, right? the back button is not the Android's one, right? – Raptor Aug 19 '14 at 09:51
  • yeah, desktop browsers are in question, ff, chrome, and maybe also ie, very broad supprot not required :) – a966821 Aug 19 '14 at 09:53

1 Answers1

0

One way to do this is to append a number at the end of your image source:

<img src="image.jpg?1" />

If your last image with the same name is image.jpg your number after ? will force the browser to download the image

So If your last image with the same name is:

<img src="image.jpg?1" />

You have to change it to

<img src="image.jpg?2" />

You can have a random generator number and put it at the end src, so every time your browser start to download your image

Hadi Rasekh
  • 2,622
  • 2
  • 20
  • 28
  • I can not control the img tag dynamically, no scripting on the blog... I have to write the img tag beforehand, and then I hope to be able to change, what the tag points to – a966821 Aug 19 '14 at 09:58
  • did you see this link? (http://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header) – Hadi Rasekh Aug 19 '14 at 10:49
  • hmm, this seems to be about a different kind of problem, dont'd see how it could help me – a966821 Aug 19 '14 at 11:04
  • did you tried to use PHP output content type? `header('Content-Type: image/jpeg');` use PHP function to output your image. functions like `imagejpeg`. you can use `SimpleImage` which is available in `https://gist.github.com/miguelxt/908143` – Hadi Rasekh Aug 23 '14 at 05:13
  • In fact I tested with a svg image that was preprocessed and outputted by php (svg is xml basically), so I indeed outputed the image from php and used content-type to set it to image, "image/svg+xml" – a966821 Aug 31 '14 at 06:41