PHPBuilder - Rotating Advertisement Script



RSS Twitter
Snippets Other

Rotating Advertisement Script

by: Bill Pellowe
|
September 3, 2003

Version: 1.1

Type: Full Script

Category: Other

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).



See http://www.i-fubar.com/rotation-ad-script.php for:
* working version
* troubleshooting tips 
* using this with .html pages
* JavaScript options 

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"; 
$bannerCounter++;

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 quote mark, like this: \" 
$bannerCode[$bannerCounter] = "<A HREF=\"http://www.amazon.com/exec/obidos/redirect-home/teijn21-20\">
<IMG SRC=\"home-banner-468x60.gif\" BORDER=\"0\" HEIGHT=\"60\" WIDTH=\"468\" alt=\"In Association with Amazon.com\"></A>"; 
$bannerCounter++;

$bannerCode[$bannerCounter] = "<a href=\"http://www.vmcsatellite.com/?aid=55537\" target=\"_top\"> <img src=\"http://www.vmcsatellite.com/banners/468x60(2).gif\" width=\"468\" height=\"60\" alt=\"Free Satellite TV!\" border=\"0\"></a>"; 
$bannerCounter++;

$bannerCode[$bannerCounter] = "<a href=\"http://www.vmcsatellite.com/channels/affiliates.cfm?aid=55537\" target=\"_top\"> <img src=\"http://www.vmcsatellite.com/banners/recruit395x141.gif\" width=\"395\" height=\"141\" alt=\"Free Satellite TV!\" border=\"0\"></a>"; 
$bannerCounter++;


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 advertisement. This may seem like a stupid feature for a random advert generator, but it could happen if you drop non-performing affiliates or if paid-inclusion ads expire.) 
$bannerAdTotals = $bannerCounter - 1; 
if($bannerAdTotals>1) 
{
   mt_srand((double)microtime() * 1234567); 
   $bannerPicked = mt_rand(1, $bannerAdTotals); 
}
else
{
   $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: 
<? 
include_once("adverts.php"); 
echo "<div align='center'>$bannerAd</div>";
?>
That's all!

See http://www.i-fubar.com/rotation-ad-script.php for:
* working version
* troubleshooting tips 
* using this with .html pages
* JavaScript options

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Bill Pellowe

Comment:



Comment:

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