Banner ad rotators can range in complexity from simple systems that randomly display banner or sidebar ads, to very complex systems that track every click on each ad and display different ads based upon preset percentages for each ad display.
This snippet of PHP code is the simple version. As many ads as you want can be loaded into an array and the code will randomly select an ad to display every time the page is loaded. This is a good solution for someone who wants to randomly display either advertising links or images on a page. It works well with banner image links found with many banner exchange programs and affiliate marketing programs. Simply substitute the code provided by your affiliate network or advertising partners for the value part of the name-value pairs in the following code.
Here is the code for the simple PHP banner ad rotator:
<?php $bannerAd = 'code for ad 1'; $bannerAd = 'code for ad 2'; $bannerAd = 'code for ad 3'; $bannerAd = 'code for ad 4'; $bannerAd = 'code for ad 5'; $adCount = count($bannerAd); $randomAdNumber = mt_rand(1, $adCount); echo $bannerAd[$randomAdNumber]; ?>
Here’s how it works:
The following is an example of the hyperlink code for an advertising link. The code should be on a single line in your script.
$bannerAd = '<a href="http://www.tech-evangelist.com/" target="_blank"><img src="/images/banner01.gif" width="468" height="60" border="0"></a>';
If you want to use this script to randomly display images, just eliminate the hyperlink code and use the image portion of the code like the following.
$bannerAd = '<img src="/images/banner01.gif" width="468" height="60" border="0">';
Remember to place this on a single line in your script. 😀