dsaba Posted June 5, 2008 Share Posted June 5, 2008 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! Quote Link to comment Share on other sites More sharing options...
dsaba Posted June 5, 2008 Author Share Posted June 5, 2008 bump Quote Link to comment Share on other sites More sharing options...
flyhoney Posted June 5, 2008 Share Posted June 5, 2008 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" /> Quote Link to comment Share on other sites More sharing options...
kev wood Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
dsaba Posted June 5, 2008 Author Share Posted June 5, 2008 did anyone actually read my post? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted June 5, 2008 Share Posted June 5, 2008 Yes, I know you dont want to use a query string, but we're saying that may be the only way. It's not a bad solution either. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
dsaba Posted June 5, 2008 Author Share Posted June 5, 2008 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? Quote Link to comment Share on other sites More sharing options...
jonsjava Posted June 5, 2008 Share Posted June 5, 2008 sounds to me like you're asking in the wrong forum. maybe you should head over to the HTML Help section to get a better answer. The PHP is working fine. The browser isn't. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted June 5, 2008 Share Posted June 5, 2008 You could load the image with Javascript individually, this might force them to reload properly. Quote Link to comment Share on other sites More sharing options...
dsaba Posted June 5, 2008 Author Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
discomatt Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 5, 2008 Share Posted June 5, 2008 Yeah, it didn't work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.