First of all let me provide an example for you:
Random Background
If you click the link above and then refresh the target page you will see that the image will change each time (one among all the images in the folder will be selected).
I use that script to change the header of my website, so each page will have a different header background:
Annunci Di Matrimonio
How to achieve that?
It is really easy.
- Create a folder on your website and put all images you want to rotate (this script is based upon JPG images, but it won't be tough to be adapted to other images).
- Create a php file (choose the name you like... random_bg.php for example) with this code in it:
<?php
$img_array = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');
$img_rnd = rand(0, sizeof($img_array) - 1);
header('Content-type: image/jpg');
header('Content-Disposition: filename=' . $img_array[$img_rnd]);
readfile($img_array[$img_rnd]);
?>
- Make sure the array at the beginning of the file contains all the images you want to rotate:
$img_array = array('bg_01.jpg', 'bg_02.jpg', 'bg_03.jpg', 'bg_04.jpg', 'bg_05.jpg', 'bg_06.jpg', 'bg_07.jpg', 'bg_08.jpg', 'bg_09.jpg');
- Just recall the php file from your browser to test it (this is just an example):
- If it is working fine, you can then use this image in your CSS or HTML code to output a random BG where needed!
I hope you will find this script useful!