This article will take a look at a fairly simple means of loading random images into a web browser without the need for a script. It has proven to be quite a handy little 'trick' for me, and several people have expressed interest in using it over the years. I do not claim to be the creator, but found this in use a while back with minimal documentation and decided to write it up to detail the basics.
The applications of this are about as vast as the Internet itself, but some instances where it might come in handy include:
» Random blog or website logos
» Dynamic forum signatures
» To draw attention to a static text block like a list or a menu
» Banner ad rotation for a single advertiser with multiple graphics
While there isn't a script to install for rotating images with this method, there are a few simple things you will need in order to make it work. You won't be able to do this with images hosted on a free uploader site, but with even the most basic of Apache
web server hosting accounts this should work perfectly.
(a Zip file example of the steps covered is available at the end of the article)
1. The first thing I would suggest is to create a folder on your web hosting account for all the images to be rotated. While a separate folder might not be 100% necessary, it keeps things organized and may prevent interference with other aspects of your site. For this article, the folder created will be called "rotate".
2. Next we need to create a .htaccess file to be uploaded to the folder containing all of the images. The number of things an .htaccess can do on a server is quite amazing, but what we are interested in is letting it know that a certain image file in this folder is actually to be recognized as a bit of PHP code. The .htaccess should contain the following text...
Simply copy that text to a text editor and save the file as .htaccess - then upload it to the folder created for your images. The reference to "example.gif" is the name of the image file that the server needs to identify as a actually containing PHP code. The name of the .gif is unimportant, but we'll be using example.gif for the purposes of this article.
3. Now we need to create example.gif, and there is no need for an image editing application for this, just a text editor. If your text editor of choice refuses to save to a .gif extension, simply make a file called example.txt and rename it to example.gif in your file manager. The contents of example.gif should include...
$dh = opendir(".");
while (false !== ($file = readdir($dh)))
if (preg_match('/\.(jpg|gif|png)$/i', $file) and $file != "example.gif")
$filelist = $file;
$picnum = rand(0, sizeof($filelist) - 1);
header("Location: " . $filelist[$picnum]);
The structure of example.gif tells the server to look for files within the rotate folder that start with "example", followed by a number, and that end with any of the image file extensions listed. The fifth line of the code provides the allowable image extensions, (jpg|gif|png), and you can edit this to meet your needs. The initial implementation of this that I found only listed jpg and gif files, (jpg|gif), but .png files work fine as well.