Refresh page without cache?

Associate
Joined
15 May 2006
Posts
389
Basically what I want to achieve is when a user refreshs the page (normal refresh, not control+F5) an image changes.

The page is flat .html

Im using this script http://photomatt.net/scripts/randomimage/

However when i refresh, it seems its pulling the image from cache, not requesting it from the server again.

How do i go about this?

Also if i need to use php to do it, how do i change my html page to a php page? Or could I rotate the image with some sort of java script instead of the php script above?

Thanks for any advice!
 
Appending a timestamp to the end of the image path works as a per-second pseudo-cache. Alternatively you can use microtime() or a random string generator.

Change the .html to .php, and then do like so:

PHP:
<img src="myimage.jpg?<?php echo time(); ?>" width="XX" height="XX" alt="blah">

Edit: it seems the cached HTML file being served is the problem. As HTTP cache headers are never going to be interpreted 100% correctly, replicate the same behaviour with something like:

PHP:
<?php

if(!isset($_GET['random']))
	header('location: thisfile.php?random=' . time());

?>

at the top of your file. Then when you serve thisfile.php, if there is no timestamp appended to the query string, it will do so, and as this is random the next page request with a new timestamp will not have been cached.
 
Last edited:
psyr33n said:
Appending a timestamp to the end of the image path works as a per-second pseudo-cache. Alternatively you can use microtime() or a random string generator.

Change the .html to .php, and then do like so:

PHP:
<img src="myimage.jpg?<?php echo time(); ?>" width="XX" height="XX" alt="blah">

Edit: it seems the cached HTML file being served is the problem. As HTTP cache headers are never going to be interpreted 100% correctly, replicate the same behaviour with something like:

PHP:
<?php

if(!isset($_GET['random']))
	header('location: thisfile.php?random=' . time());

?>

at the top of your file. Then when you serve thisfile.php, if there is no timestamp appended to the query string, it will do so, and as this is random the next page request with a new timestamp will not have been cached.

so im replacing the call to the image in my html document with

PHP:
<img src="myimage.jpg?<?php echo time(); ?>" width="XX" height="XX" alt="blah">

and also renaming it from .html to .php


then at the very start of the php script itself im adding

PHP:
<?php

if(!isset($_GET['random']))
	header('location: thisfile.php?random=' . time());

?>

is that correct?
 
I use this code for a rotating image script to prevent caching. Probobly long and drawn out but its highly effective.

Code:
<?php
session_start();
$_SESSION['AntiCache'] = $_SESSION['AntiCache'] + 1;

	function randstr($len = 8) {
		$pos = "abcdefghijklmnopqrstuvwxyz0123456789";
		$i = 0;
		$output = "";
			while ($i < $len) {
				if (strstr($output, $pos) === FALSE) {
					$output .= substr($pos, mt_rand(0, strlen($pos) - 1), 1);
					$i++;
				}
			}
		return $output;
	}
	
	if ($_SESSION['AntiCache'] == 2) {
		header('Location: ' . $_SERVER['SCRIPT_NAME'] . "?ac=" . randstr());
		$_SESSION['AntiCache'] = 0;
		die();
	}

?>
 
Back
Top Bottom