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

How to keep from cacheing images...

P: n/a
I have a script that outputs images directly using the 'image' functions.
I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic
images.

Norm
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Norman Peelman wrote:
I have a script that outputs images directly using the 'image'
functions.
I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic
images.


This should do it:

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

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2

P: n/a
"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:10**************@216.128.74.129...
Norman Peelman wrote:
I have a script that outputs images directly using the 'image'
functions.
I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic images.


This should do it:

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

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/


Still not working: I want to do this...

HTML page
-------------

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
</BODY>
</HTML>
myimage.php can return a random image regardless of what image=21 is set
to and I want to see different images on the same page where the
<IMG SRC="http://my.domain.com/myimage.php?image=21"> tag appears more than
once. I have no option to change the image=21 value after it is inserted
into the html. I keep seeing the same image for all occurances. Refreshes
will change the image but that is really not the desired effect.

Norm
Jul 17 '05 #3

P: n/a
Norman Peelman wrote:
"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:10**************@216.128.74.129...
Norman Peelman wrote:
> I have a script that outputs images directly using the 'image'
> functions.
> I have googled quite a bit but cannot seem to find the correct method
> of sending headers to keep the images from being cached... these are dynamic > images.


This should do it:

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

Still not working: I want to do this...

HTML page
-------------

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
</BODY>
</HTML>
myimage.php can return a random image regardless of what image=21 is set
to and I want to see different images on the same page where the
<IMG SRC="http://my.domain.com/myimage.php?image=21"> tag appears more
than
once. I have no option to change the image=21 value after it is inserted
into the html. I keep seeing the same image for all occurances. Refreshes
will change the image but that is really not the desired effect.


It's not going to work. The browser will see that it's supposed to display
the same image http://my.domain.com/myimage.php?image=21 four times so it's
only going to request the image once and display it four times.

If you want all four images to be different why don't you do something like
eg:

<IMG SRC="http://my.domain.com/myimage.php?image=21&foo=1">
<IMG SRC="http://my.domain.com/myimage.php?image=21&foo=2">
<IMG SRC="http://my.domain.com/myimage.php?image=21&foo=3">
<IMG SRC="http://my.domain.com/myimage.php?image=21&foo=4">

The "foo" value doesn't need to mean anything, but it will mean the browser
has to download all 4 instances as the URLs are all unique.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #4

P: n/a
"Norman Peelman" <np******@cfl.rr.com> escreveu na mensagem
news:ys*******************@twister.tampabay.rr.com ...
I have a script that outputs images directly using the 'image' functions. I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic
images.

Norm


Strangely i have the opposite situation, my site (http://ntg.docaj.net)
seems to be trying to fetch the images on every page reload (refresh)
despite the fact that the image name is always the same (check the ranking
on the right side for instance, the red and green png images are always
download on every refresh).

How to prevent it? using clients cache?

Thanks,
RootShell
Jul 17 '05 #5

P: n/a
Add a random query string at the end of your URL and that should do it.
What is happening is that your browser is caching, not necessarily the
server.

before you output anything try this as well:

clearstatcache();

then add your headers as Chris Hope suggested.. then in your HTML do this:

<IMG SRC="http://mydomain.com/myimage.php?image=21&junk=<?= randstring(16)
?>">

Where randstring() is a function you write that outputs a random
alphanumeric string of a-zA-Z0-9

Phil
"Norman Peelman" <np******@cfl.rr.com> wrote in message
news:nl*********************@twister.tampabay.rr.c om...
"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:10**************@216.128.74.129...
Norman Peelman wrote:
I have a script that outputs images directly using the 'image'
functions.
I have googled quite a bit but cannot seem to find the correct method of sending headers to keep the images from being cached... these are dynamic images.
This should do it:

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

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/


Still not working: I want to do this...

HTML page
-------------

<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
<IMG SRC="http://my.domain.com/myimage.php?image=21">
</BODY>
</HTML>
myimage.php can return a random image regardless of what image=21 is set
to and I want to see different images on the same page where the
<IMG SRC="http://my.domain.com/myimage.php?image=21"> tag appears more

than once. I have no option to change the image=21 value after it is inserted
into the html. I keep seeing the same image for all occurances. Refreshes
will change the image but that is really not the desired effect.

Norm

Jul 17 '05 #6

P: n/a

"RootShell" <AN***************@Netcabo.ptANTISPAM> wrote in message
news:40***********************@news.telepac.pt...
"Norman Peelman" <np******@cfl.rr.com> escreveu na mensagem
news:ys*******************@twister.tampabay.rr.com ...
I have a script that outputs images directly using the 'image'

functions.
I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic images.

Norm


Strangely i have the opposite situation, my site (http://ntg.docaj.net)
seems to be trying to fetch the images on every page reload (refresh)
despite the fact that the image name is always the same (check the ranking
on the right side for instance, the red and green png images are always
download on every refresh).

How to prevent it? using clients cache?


That's correct behavior. The browser is supposed to revalidate all files
when the user requests a page refresh.
Jul 17 '05 #7

P: n/a
"Norman Peelman" <np******@cfl.rr.com> wrote in message
news:ys*******************@twister.tampabay.rr.com ...
I have a script that outputs images directly using the 'image' functions. I have googled quite a bit but cannot seem to find the correct method of
sending headers to keep the images from being cached... these are dynamic
images.

Norm


It seems my second post got lost somewhere - here it is again. Let's say I
have a HTML file like so:

<HTML>
<HEAD>
<TITLE>Image Test</TITLE>
</HEAD>
<BODY>
<IMG SRC="http://www.mydomain.com/myscript.php?img=21">
<IMG SRC="http://www.mydomain.com/myscript.php?img=21">
<IMG SRC="http://www.mydomain.com/myscript.php?img=21">
<IMG SRC="http://www.mydomain.com/myscript.php?img=21">
</BODY>
</HTML>

---

Now, 'myscript.php' is coded such that depending on the value of 'img' it
can return a random image (using the PHP/GD 'image' functions). I would
like to be able to have the above HTML page display four random images upon
each view/refresh. I do not have the ability to alter the SRC attribute as
it may appear only once or ten times in a single web page, I don't know.
Currently I do see random images upon each refresh but all four images
displayed are the same. Is there no headers that I can send that would tell
the browser to ask for the <IMG> each time it encounters it.

So, in short, I know that my script works fine. There just seems to be the
issue of not being able to tell the browser to send a seperate request for
each <IMG> encountered. That is my desired effect.

Thank you,

Norm
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.