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

Page still caches even with header() and random query string - HELP

P: n/a
I created a page that will be doing image resizing and manipulation,
which seems to work (using GD library). However, upon returning to
the page where the image has been changed, I still see the old image,
until I refresh my browser and then zappo, there it is! All changed!

I have done everything I can think of to force caching including this:

[PHP]
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");
[/PHP]

And even including a "junk" query string value that returns a random
string of characters:

Expand|Select|Wrap|Line Numbers
  1. http://.../..index.php?id=$id&section=$section&action=$action&junk=<?=
  2. randString(16); ?>
All to no avail!! The page STILL caches no matter what I do, only to
show the changed image if I refresh my browser.

The URL will always be "index.php" for ease of architectural build
(using query string values to deliver the right classes and methods to
perform whatever action is necessary). Again, everything works, just
can't view my changed image unless I manually refresh my browser.

Any other ideas?

Phil
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
[ Followup-To: comp.lang.php ]

Phil Powell wrote:
I created a page that will be doing image resizing and manipulation,
which seems to work (using GD library). However, upon returning to
the page where the image has been changed, I still see the old image,
until I refresh my browser and then zappo, there it is! All changed!

I have done everything I can think of to force caching including this: (snip) Any other ideas?


Are you putting the cache-control stuff /within/ the image generation
script?

Something like:

<?php
// create the image
$image = imagecreate();
// ...

// prevent caching for *this* image
header('Expires: ...');
header('Last-Modified: ...');
header('Cache-Control: ...');
header('Pragma: ...');

// send the image to the browser
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
[ Followup-To: comp.lang.php ]

Phil Powell wrote:
I created a page that will be doing image resizing and manipulation,
which seems to work (using GD library). However, upon returning to
the page where the image has been changed, I still see the old image,
until I refresh my browser and then zappo, there it is! All changed!

I have done everything I can think of to force caching including this:


(snip)
Any other ideas?


Are you putting the cache-control stuff /within/ the image generation
script?


Usually, I don't even bother with that because the random query string
usually does the trick for me. Of course, I tend to put the random part
at the beginning of the URI like:

<img src="img_gen.php?j=fjgs749Dj4FotFDJ&id=<?php echo $id
?>&section=<?php echo $section ?>&action=<?php echo $action ?>"
alt="Generated Image">

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Official Google SERPs SEO Competition: http://www.koivi.com/serps.php
Jul 17 '05 #3

P: n/a
This script works for me
browser is MozillaFirebird / Win2K;
server is PHP 4.3.3 / Apache 1.3.29

#v+
<?php
header('Expires: 0');
header('Cache-Control: no-cache, no-store, private');
header('Pragma: no-cache');

$d = date('Ymd');
$h = date('H:i:s');
list($u, $dummy) = explode(' ', microtime());
$u = substr($u . '000000', 0, -8);

if (isset($_GET['img'])) {

$im = imagecreate(90, 60);
$r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255);
$bgcolor = imagecolorallocate($im, $r, $g, $b);
$black = imagecolorallocate($im, 255-$r, 255-$g, 255-$b);
imagestring($im, 5, 5, 2, $d, $black);
imagestring($im, 5, 5, 22, $h, $black);
imagestring($im, 5, 5, 42, $u, $black);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
exit();

}

echo <<<HTML
<html>
<head>
<title>image test</title>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<body>
<img src="imgtest.php?img&u=$u" alt="date/time as image"/><br/>
<a href="">refresh</a> @ $d $h $u<br/>
See just the <a href="imgtest.php?img&u=$u">image</a> and then hit the
&quot;Back&quot; button.
</body></html>
HTML;
?>
#v-
Hope you can make it work for you too :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4

P: n/a
Actually using the header('Content-type: image/png'); type command caused my
entire index.php to be converted to a PNG image and caused severe HTTP
errors to generate.

Nothing worked.. until I tried something very very simple:

<img src=blah.jpg?junk=<?= randString(16) ?>>

That did it!!

Phil

"Pedro Graca" <he****@hotpop.com> wrote in message
news:bv************@ID-203069.news.uni-berlin.de...
This script works for me
browser is MozillaFirebird / Win2K;
server is PHP 4.3.3 / Apache 1.3.29

#v+
<?php
header('Expires: 0');
header('Cache-Control: no-cache, no-store, private');
header('Pragma: no-cache');

$d = date('Ymd');
$h = date('H:i:s');
list($u, $dummy) = explode(' ', microtime());
$u = substr($u . '000000', 0, -8);

if (isset($_GET['img'])) {

$im = imagecreate(90, 60);
$r = rand(0, 255); $g = rand(0, 255); $b = rand(0, 255);
$bgcolor = imagecolorallocate($im, $r, $g, $b);
$black = imagecolorallocate($im, 255-$r, 255-$g, 255-$b);
imagestring($im, 5, 5, 2, $d, $black);
imagestring($im, 5, 5, 22, $h, $black);
imagestring($im, 5, 5, 42, $u, $black);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
exit();

}

echo <<<HTML
<html>
<head>
<title>image test</title>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
</head>
<body>
<img src="imgtest.php?img&u=$u" alt="date/time as image"/><br/>
<a href="">refresh</a> @ $d $h $u<br/>
See just the <a href="imgtest.php?img&u=$u">image</a> and then hit the
&quot;Back&quot; button.
</body></html>
HTML;
?>
#v-
Hope you can make it work for you too :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--

Jul 17 '05 #5

P: n/a
Phil Powell wrote upsidedown:
Actually using the header('Content-type: image/png'); type command caused my
entire index.php to be converted to a PNG image
More likely it caused your browser to treat the data as a PNG image.
No conversion took place.
and caused severe HTTP errors to generate.
Which were?

[Concerning non-caching of images:]
Nothing worked.. until I tried something very very simple:

<img src=blah.jpg?junk=<?= randString(16) ?>>
(That'll result in invalid markup, in every version of HTML to date.)
That did it!!


That suggestion is common, but is definitely no substitute for the
proper method, which has already been suggested: send the appropriate
headers, for *all* files.

Rather than going down the road of dynamic URLs, I reckon you'd be
better off appending a static query string to the image's URL (or just
a "?"); a dynamic, "once-only" URL (containing a query string) will
only needlessly clog up cache proxy servers that store URLs with query
components. And there are some of those, I believe. If you want to
defeat caches using unique URLs, you ought to ensure that the response
isn't cacheable. What's the point in having multiple copies of the
same image stored in caches?

The reason for appending a query string at all is that "caches MUST
NOT treat responses to [URLs with "?" in the path] as fresh unless the
server provides an explicit expiration time" (RFC2616, sec. 13.9). So
some systems check for the presence of a "?" and, if found, consider
the response uncacheable. Those cache proxy servers would treat the
response as stale and request a new copy from the origin server. That
said, I stand by my assertion that you ought to send appropriate
headers for every file, whether you use the query trick or not.

Without appropriate cache headers sent with the HTML file, dynamic
image URLs are pointless. A browser would fetch the HTML file from a
cache somewhere along the lines, and retrieve the image using the same
old URL -- probably from a cache too, if the image's response headers
contained the relevant caching instructions. That's why it's
important to send appropriate headers.

Trial and error techniques aren't very helpful; it's better to
understand the goings-on. M. Nottingham's "Caching Tutorial for Web
Authors and Webmasters" is the much-publicised article on the subject:

http://www.mnot.net/cache_docs/

The following is an excerpt from that document, in response to the FAQ
"My images expire a month from now, but I need to change them in the
caches now!":

The most effective solution is to rename the files; that way, they
will be completely new objects, and loaded fresh from the origin
server. Remember that the page that refers to an object will be
cached as well. Because of this, it's best to make static images
and similar objects very cacheable, while keeping the HTML pages
that refer to them on a tight leash.

The HTTP-EQUIV nonsense, as usual, isn't worthy of consideration.

--
Jock
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.