Forum Moderators: coopster

Message Too Old, No Replies

no cache on random image

stop caching of random image

         

ahmedtheking

5:00 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



this random image script works:

<?php

// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = 'home page/';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = 1; // Initialize some variables
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false!== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

Header ('Location: '.$folder.$files[$rand]);

?>

but, is there a way to make sure that the images cannot get cached and always change?

[edited by: coopster at 7:14 pm (utc) on Aug. 5, 2004]
[edit reason] removed url [/edit]

pete_m

5:04 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



One thing to try: append
"?" . time()
to the end of the filename.

ahmedtheking

5:55 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Header ('Location: '.$folder.$files[$rand]"?".time());

?>

wot so its like that?

or to the

<img src=randomimage.php"?".time()>

(sorry bit dense!)

pete_m

6:20 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



I'd add it to the HTML:
<img src="randomimage.php?<?php echo(time());?>" />

As long as the HTML is getting reloaded each time, the image should change.

uncle_bob

8:22 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Have a look at mod_expires, at set the random/image to expire immediately. That should force the browser to check for a new version every time it loads the page.