Connecting Tech Pros Worldwide Forums | Help | Site Map

How to force browser to reload image from server?

Martin
Guest
 
Posts: n/a
#1: Jul 17 '05
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">



Daniel Tryba
Guest
 
Posts: n/a
#2: Jul 17 '05

re: How to force browser to reload image from server?


Martin <martinvalley@comcast.net> wrote:[color=blue]
> How can I force the browser to reload an image from the server?[/color]

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

Quick fix messageid: <1n2f93lvdreny$.yqiy0gnsfyfx.dlg@40tude.net>

--

Daniel Tryba

Chung Leong
Guest
 
Posts: n/a
#3: Jul 17 '05

re: How to force browser to reload image from server?


"Martin" <martinvalley@comcast.net> wrote in message
news:2f83l0hvjgqhr0a645400cdg6oi2u6k5ai@4ax.com...[color=blue]
> 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.[/color]

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.


Simon Stienen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: How to force browser to reload image from server?


Chung Leong <chernyshevsky@hotmail.com> wrote:[color=blue]
> "Martin" <martinvalley@comcast.net> wrote in message
> news:2f83l0hvjgqhr0a645400cdg6oi2u6k5ai@4ax.com...[color=green]
>> 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.[/color]
>
> 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.[/color]

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
Martin
Guest
 
Posts: n/a
#5: Jul 17 '05

re: How to force browser to reload image from server?


On Thu, 23 Sep 2004 09:05:34 +0200, Simon Stienen
<simon.stienen@news.slashlife.de> wrote:
[color=blue]
>Chung Leong <chernyshevsky@hotmail.com> wrote:[color=green]
>> "Martin" <martinvalley@comcast.net> wrote in message
>> news:2f83l0hvjgqhr0a645400cdg6oi2u6k5ai@4ax.com...[color=darkred]
>>> 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.[/color]
>>
>> 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.[/color]
>
>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...)[/color]

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.

[color=blue]
>
>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.[/color]

Closed Thread


Similar PHP bytes