PHPBuilder - Rotating Advertisement Script

RSS Twitter

Rotating Advertisement Script

by: Bill Pellowe
August 16, 2003

Version: 1.1

Type: Full Script

Category: File Management

License: GNU General Public License

Description: Randomly rotate your advertisements. Works without a database and without cookies. Handles banners, text-based ads, whatever format. Includes troubleshooting tips for .html files (either follow the tutorial for .htacess editing or use the JavaScript workaround). Free.

For working version, updated code and troubleshooting tips, see

First, create a file with a name like adverts.php and have this at the top: 
$bannerCounter= 1; 

For each of your advertisements, you'll first write out the ad's code, and then increase the banner counter, like this: 
$bannerCode[$bannerCounter] = "put your ad code here"; 

For the example below, let's do a 3-banner rotation. Note that every \" in your ad code needs to be \"escaped\" with a slash mark in front of the code. 
$bannerCode[$bannerCounter] = "<A HREF=\"\">
<IMG SRC=\"home-banner-468x60.gif\" BORDER=\"0\" HEIGHT=\"60\" WIDTH=\"468\" alt=\"In Association with\"></A>"; 

$bannerCode[$bannerCounter] = "<a href=\"\" target=\"_top\"> <img src=\"\" width=\"468\" height=\"60\" alt=\"Free Satellite TV!\" border=\"0\"></a>"; 

$bannerCode[$bannerCounter] = "<a href=\"\" target=\"_top\"> <img src=\"\" width=\"395\" height=\"141\" alt=\"Free Satellite TV!\" border=\"0\"></a>"; 

Next, we get the total number of ads, and if there are more than one, we'll randomly pick one of them. (If less than one, we automatically pick the first one.) 
$bannerAdTotals = $bannerCounter - 1; 
   mt_srand((double)microtime() * 1234567); 
   $bannerPicked = mt_rand(1, $bannerAdTotals); 
   $bannerPicked = 1; 
$bannerAd = $bannerCode[$bannerPicked]; 
That's the end of the adverts.php file.
Now, your webpage showing the ad first needs to include this script, then show the ad, like this: 
echo "<div align='center'>$bannerAd</div>
That's all!

For working version, updated code and troubleshooting tips, see

Comment and Contribute

Your comment has been submitted and is pending approval.

Bill Pellowe



(Maximum characters: 1200). You have characters left.