Cookie-based PHP image rotator(non-random)

Posted 2082 days ago by Alex in Randomness

As a reply to a post on the x10Hosting.com Forums, I made a simple little script that rotates images using PHP. I searched online for a script that did it, but could only find ones that rotated images randomly… which isn’t really rotating them at all.

I did find a snippet of code which generated a random image other that the one that was shown previously (eliminating displaying of duplicate images). I realized this wouldn’t be a bad way to store the last image displayed for the next time the script was run.

In my code, you specify the array of images you wish to display and store them in their desired order. Whenever the file is called, it will read a cookie on the client’s computer as to which image was displayed last. If the cookie isn’t present, it is created. Then it simply displays the next image in the array of images. Don’t worry about overflowing out of the bounds of the array, when the last image is shown, the next one up is the first image.

Go here to view the code: http://alex.elementfx.com/_scripts/rotator.php