I am going to make a simple php image rotator that can also be used as a banner rotator.
First, create a new file called “images.txt”.
In this file, write embed code for your images, one per line, like:


<img src="path/url to image" alt="" />
<img src="path/url to image" alt="" />

Or you can write link codes, just in case you want to link the images to a page, or use it like a banner rotator, again one per line, like so:


<a href="link"><img src="path/url to image" alt="" /></a>
<a href="link"><img src="path/url to image" alt="" /></a>

php code for .php file


<?php
//open function
function random_image() {
//seed the random number generator
//only for php versions under 4.2.0
//if the version on your server is over 4.2.0 u can ignore
//the next line of code
srand((float) microtime() * 10000000);
//set data file name/path and read it
$f=file_get_contents('images.txt');
//create an array with all the images we found
$input = explode("\n",$f);
//create an array of keys for the random entries
//the number has to be between 1 and the number of 
//elements in the array
$rand_keys = array_rand($input, 2);
//show a random image
echo $input[$rand_keys[0]];
//close function
}
//call the function and print the images
random_image();
?>
Simple php image rotator

Leave a Reply

Your email address will not be published.