By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

How to force browser to reload image from server?

P: n/a
This is probably more of an HTML question than PHP. Perhaps someone
here can answer or point me to a proper newsgroup.

How can I force the browser to reload an image from the server?

I have a page where the user can set some parameters and click a
<submit> button. A PHP script then generates a .png graphic and saves
it to disc. An html statement (<p><img src="gMyGraphic.png"></p>) then
sends the graphic out to the browser.

This works as expected - the first time. But when the user modifies a
parameter and re-submits the page, the modified graphic does not get
sent to the browser. I know the graphic is being updated - I can look
at it on the server's hard drive (and I'm displaying some other
information on the page that indicates it's been updated).

If I "refresh" the page in the browser, then the modified graphic IS
displayed.

It appears that the browser is using a cached copy of the graphic -
even though I've told it not to cache anything. The page includes the
following statements:

<META HTTP-EQUIV="expires" CONTENT="Wed, 09 Aug 2000 08:21:57 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Martin <ma**********@comcast.net> wrote:
How can I force the browser to reload an image from the server?


See the thread 'Image not refreshing' started on the 16th bij Ken.
(http://groups.google.com/gr*********....rdc-kc.rr.com)

Quick fix messageid: <1n*****************************@40tude.net>

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
"Martin" <ma**********@comcast.net> wrote in message
news:2f********************************@4ax.com...
This is probably more of an HTML question than PHP. Perhaps someone
here can answer or point me to a proper newsgroup.

How can I force the browser to reload an image from the server?

I have a page where the user can set some parameters and click a
<submit> button. A PHP script then generates a .png graphic and saves
it to disc. An html statement (<p><img src="gMyGraphic.png"></p>) then
sends the graphic out to the browser.


The easiest way is to stick a random number into the URL:

<img src="gMyGraphic.png?<? echo rand(1,3000); ?>">

Consider outputting the image data directly from the PHP script to the
browser.
Jul 17 '05 #3

P: n/a
Chung Leong <ch***********@hotmail.com> wrote:
"Martin" <ma**********@comcast.net> wrote in message
news:2f********************************@4ax.com...
This is probably more of an HTML question than PHP. Perhaps someone
here can answer or point me to a proper newsgroup.

How can I force the browser to reload an image from the server?

I have a page where the user can set some parameters and click a
<submit> button. A PHP script then generates a .png graphic and saves
it to disc. An html statement (<p><img src="gMyGraphic.png"></p>) then
sends the graphic out to the browser.


The easiest way is to stick a random number into the URL:

<img src="gMyGraphic.png?<? echo rand(1,3000); ?>">

Consider outputting the image data directly from the PHP script to the
browser.


time() instead of rand() should suffice and is even more easier... and you
won't get the old image by random... (you have a 1 in 3000 chance to see
the old image again...)

Maybe a bit more complex, but even better than rand() or time() would be
<img src="myimage.png?<?php echo filemtime('myimage.png') ?>">
since it doesn't *completely* turn off caching.

--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #4

P: n/a
On Thu, 23 Sep 2004 09:05:34 +0200, Simon Stienen
<si***********@news.slashlife.de> wrote:
Chung Leong <ch***********@hotmail.com> wrote:
"Martin" <ma**********@comcast.net> wrote in message
news:2f********************************@4ax.com...
This is probably more of an HTML question than PHP. Perhaps someone
here can answer or point me to a proper newsgroup.

How can I force the browser to reload an image from the server?

I have a page where the user can set some parameters and click a
<submit> button. A PHP script then generates a .png graphic and saves
it to disc. An html statement (<p><img src="gMyGraphic.png"></p>) then
sends the graphic out to the browser.
The easiest way is to stick a random number into the URL:

<img src="gMyGraphic.png?<? echo rand(1,3000); ?>">

Consider outputting the image data directly from the PHP script to the
browser.


time() instead of rand() should suffice and is even more easier... and you
won't get the old image by random... (you have a 1 in 3000 chance to see
the old image again...)


Good suggestion - thanks.

I added the rand(1,100) and it worked but, like you say, I was getting
an old image every once in a while. I changed it to rand() which
eliminated that but the potential is still there. With time(), it's
guaranteed to always be a different number.


Maybe a bit more complex, but even better than rand() or time() would be
<img src="myimage.png?<?php echo filemtime('myimage.png') ?>">
since it doesn't *completely* turn off caching.


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.