views:

53

answers:

2

I've been doing some experimenting with php and html, and I'd like to know if it's possible for the browser to send a separate request for multiple images that have the same file name. For example, if I had

<img src="showimg.php?id=1234"> 

written out 3 different times, is it possible for the browser to send the request 3 different times, thus getting a different image each time? It seems so far that the browser recognizes that the file name is the same and then just serves up the image from the cache. I came to this conclusion by using a session variable that increments every time showimg.php is accessed. If the id is different then the counter gets incremented 3 times. But if all three id's are the same then the counter only gets incremented once no matter how many times I request the image. Since I am no authority on browsers, I was hoping someone could provide a concrete answer.

EDIT: My question is: How can I force the browser to load a different image using the same id. I know it can be done with unique ids. I'm just wondering if this is possible.

Thank you to those who have responded so far, though.

+1  A: 

You can disable caching for showimg.php by specifying Cache-Control header in showimg.php:

<?php header("Cache-Control: no-cache"); ?>

Also you can add second attribute to showimg.php which can be ignored by script, but browser will treat it as different file.

And yes, incrementation can be managed via sessions or cookies.

Māris Kiseļovs
Yes, I have a whole bunch of headers that tell the browser not to cache images, but that only prevents it from caching them to the disk, not memory..
A: 

I think the only choice you have is to append some random string to the image to make the "file name" unique. Otherwise the browser will (rightly) assume that files with the same URL are the same image. From a REST point of view, they should be.

<img src="showimg.php?id=1234&rand=<?php echo uniqid(); ?>"> 
deceze
PHP's uniqid() for same purpose will be much better
Māris Kiseļovs
@Māris True, it probably is. :)
deceze
Forgive my lack of knowledge, but what is a "REST" point of view?
@vince [Representational State Transfer](http://en.wikipedia.org/wiki/Representational_State_Transfer) is the principle on which the web works. A URL represents a resource, and it's assumed that this resource does not randomly change, but stays constant (at least for some time). Hence the browser assumes that all images on the same page with the same URL are the same image.
deceze
Interesting.. So, is there no way to perhaps use Ajax to automatically re-initiate the request for the image once the page has loaded (and do so unbeknownst to the user)? You don't need to provide any code, I'm just wondering.
@vince I'd guess injecting an `<img>` tag via Javascript would have the same effect. You *could* force-fetch the URL via AJAX, but the only way to display the image with the resulting data would be to use Data-URIs, which is not very compatible. Is the random parameter at the end such a bad solution?
deceze
I see. No it's not such a bad solution. I'm just doing this as a proof of concept type thing. Thanks for all the info.