Jump to content

Really forcing no-cache behavior in dynamic image display in html/browser???


dsaba

Recommended Posts

I have a php page named img.php:

<?php
$s = (int) @file_get_contents('num.s');
if (empty($s)) {
$s = 0;
}

$h = fopen('num.s', 'w');
fwrite($h, $s+1);
fclose($h);

$im = imagecreatetruecolor(20, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $white); //fill image with white
imagestring($im,3,1, 1, $s, $black);

//display image
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: image/gif");
imagegif($im);
imagedestroy($im);
?>

 

As you can see it simply keeps a counter and keeps counting up, so everytime you query the page it should output a different image with an increasing number on it. Looks like the no-cache affect is working just fine when you refresh the page right?

http://bstats.freehostia.com/img.php

 

Well let's try a different approach, make another page called see_img.php:

<?php
for ($i=0; $i<=5; $i++) {
echo '<img src="img.php"/>';
}
?>

http://bstats.freehostia.com/see_img.php

 

Well now we only see the same number! How can I stop this cache like behavior?

Ah well.. what about putting a random querystring to the image, to trick the browser into thinking it is a new image:

<?php
for ($i=0; $i<=5; $i++) {
echo '<img src="img.php?i='.$i.'"/>';
}
?>

http://bstats.freehostia.com/see_img2.php

 

 

Sure this works... But I am looking for a way to keep it from caching when displaying multiples of this dynamic image WITHOUT using a querystring to the image url. Is it possible?

 

The best thing i could think of was putting this on the dynamic image:

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Type: image/gif");

 

 

But even that is not doing the trick!

Help!

Link to comment
Share on other sites

The problem is, browsers see you loading the same image 5 times.  Pretty much every browser is going to save time by displaying the cached image.  I really think the only way around this is to stick a random number on the end of the URL e.g. <img src="img.php?934729387423" />

Link to comment
Share on other sites

i had problems with images being cached on an image upload system i built i tried every header you could possible think of and was still getting the cached images displayed on some pages.  the only way i could find to get round this problem was to append a random number to the end of the file where you wanted to display them.

 

this is the only solution i could find.  my code looked something like

 

echo '<img src ="my_image.gif."?".$rand.">'

 

hope this helps.

Link to comment
Share on other sites

And fyi,  I believe your no-cache directives are working, because when you reload the page, you do get an updated image.  The problem isn't about the headers.  A browser is going to see the same image being loaded multiple times in the same document.  The browser will save time by repeating the image and not requesting it again.  However, since you have the no-cache headers being sent, the next time the page is reloaded, the image will change, but it will still be repeated for that document.

Link to comment
Share on other sites

this worked for me, and I didn't even modify your script:

<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />
<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />
<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />
<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />
<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />
<img src="http://bstats.freehostia.com/img.php?rand=<?php print rand(100,99999); ?>"><br />

I know you said no query string, but seeing how sweet and loveable you are, I'm not sweating the details too much.

Link to comment
Share on other sites

Yes, I have already assumed all these things about the browser behavior. Rather, I was hoping someone could help me find a solution to get around this. A browser usually intercepts code like HTML and CSS to act accordingly. Is it too much to ask a browser to achieve this effect? I wouldn't think so. If it is the browser that is responsible (as usually is in rendering any code). What else can I do to keep the image dynamic on each request, but to also keep the url static.  Javascript..apache redirect commands... what?

Link to comment
Share on other sites

I'm looking for something I can control only via the url of the image and from the page that produces the image. Is there a way to form a static url with javascript that can do something like this?

 

What about redirecting with apache.. you think that will help?

ie:

^(static_url).gif$ static_url.gif?var=apacheRandNumFunc()

 

I don't even know if conceptually such a statement is possible in apache mod_rewrite...even more so if this will help me with my problem.

 

Link to comment
Share on other sites

You've maybe got an idea there, using mod_rewrite. Just try to do a simple redirect, maybe it'll work (although I'm afraid it won't):

 

RewriteEngine on

RewriteRule ^image\.gif$ img.php

 

And then access the image with "image.gif" in your HTML.

Link to comment
Share on other sites

I doubt the mod rewrite will work, because it's the browser requesting the image ONCE and repeating the same image when it generates the elements on the page.

 

This method make sense... much more sense than the same image changing during the execution of a page.

 

As one said above, you could try using javascript.. leave the elements blank, and force the browser to load specific ones after the page has loaded. Once again though, that's assuming the browser doesn't check to see if it's already got the image in memory. If you get it to work I suggest testing it on a few versions of the popular browsers to make sure you get the right results.

 

You could also simply make x copies of the script, where x is the amount of times you want the image to come up on the page. Name them img1.php img2.php... ect ect. This will prevent the browser from seeing them as the same image... and if you're using a flat file or DB to track the count, the effect should still be the same.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.