So you need a simple banner ad rotator for your website. In this post I will guide you how to create one in PHP. No databases are needed for this. So first off open up your text editor or whatever program you use and create a new PHP document and call it something like banner468x60.php, this is so later you can include it into your templates so the banner shows up on your website!

The Settings

First we need to decide all the settings that we will need to include. We will need variables for the banner width and banner height. So to start of we will declare these.
  1. <?php  
  2.   
  3. // settings  
  4. $bannerWidth = 468;  
  5. $bannerHeight = 60;  

The Banners

Now we should create an array which will hold all the banner details. The details we will need are the url location, banner image url and a title. So below the settings we just declared we will need to create the array.
  1. // banner array  
  2. $bannerStore = array();  
  3.   
  4. // declare all the banners to be rotated, remember to enter your own banner details  
  5. $bannerStore[] = array(  
  6.     url => "http://www.domain1.com",  
  7.     image => "http://www.domain1.com/img/banner01.png",  
  8.     title => "Cool Website 01"  
  9. );  
  10.   
  11. $bannerStore[] = array(  
  12.     url => "http://www.domain2.com",  
  13.     image => "http://www.domain2.com/img/banner01.png",  
  14.     title => "Cool Website 02"  
  15. );  

Making It Rotate

Now we need to make it so the banners that you have setup can rotate and be displayed.
  1. // count how many banners have been setup  
  2. $count = count($bannerStore);  
  3.   
  4. // if there is 1 or more banners then output it  
  5. if ($count) {  
  6.   
  7.     // reduce the count by 1 since arrays begin at 0  
  8.     $count--;  
  9.   
  10.     // get a random number from 0 - $count  
  11.     $rand = mt_rand(0, $count);  
  12.   
  13.     // get banner details  
  14.     $url = $bannerStore[$rand]['url'];  
  15.     $image = $bannerStore[$rand]['image'];  
  16.     $title = $bannerStore[$rand]['title'];  
  17.   
  18.     // output banner  
  19.     echo "<a href='{$url}' title='{$title}'>";  
  20.     echo "<img src='{$image}' alt='{$title}' width='{$bannerWidth}' height='{$bannerHeight}' border='0'>";  
  21.     echo "</a>";  
  22. }  
  23.   
  24. ?>  

Displaying Your Banner

Now all you need to do is include() this file where you wish the banner to be displayed. Then whenever the page is reloaded a different banner will get displayed depending on how many banners you have put into rotation.

Customization

Now that you have created a simple banner rotator you can customize it if you wish. Ideas for improvement could be to change the target of the banner, add a border, maybe allow flash banners? The skies the limit. Have a play around!