Question

I am using this image rotator. It's great, but if I link to it multiple times on one page, it displays the same image. I don't know how many times it will be accessed, so using multiple identical scripts is impractical. How do I modify this script so that I can get a different image elsewhere on the page while using the same url? Is it possible?

EDIT: It turns out that there must have been some problem with the coding. I found a better code here, and it works if I use different queries on each link. Thanks to everyone who submitted answers!

Was it helpful?

Solution

Pass a different query string for each instance so that the browser retrieves it multiple times.

Your browser is seeing that each "rotated" image uses the same URL, so it caches the resulting image from the first request.

You can change this behaviour in two ways.

  1. Suffix your "rotated" image URLs with a random string from php. Eg ("rotate.php?img=my_static_image.jpg&x=12345" This means the browser makes a fresh request for each image.

  2. Make your rotate scrip redirect to a randomized url if it recives no extra parameters.

OTHER TIPS

You can work with the GET parameter 'img'.

/path/to/images/rotate.php?img=my_static_image.jpg

More about the default GET parameter can you find in there documentation : http://www.alistapart.com/articles/randomizer/

If you want to keep the URL the same then just add a no-cache header to your php script like so:

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

Hopefully any half decent browser will respect this and won't cache the first request.

Your other option is to modify your rotate.php script so that the code is contained within a function. Then you would use include('rotate.php'); at the top of your script and use your new rotate() function wherever you need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top