Dead simple banner advertisement rotation script

Date: Fri Apr 25 2014 Banner Advertisements »»»» Ad Rotation

You want to put advertising on your web site, and one easy simple way is to sign up for affiliate programs. In many cases affiliate programs offer some kind of banner advertisement you can display, and rather than paying out per pageview the affiliate programs of course pay if the user does something like click on an ad and then buy something. It's easy to become signed up with dozens of programs and if so it then becomes tricky to manage the advertisements. Further it is helpful to make it easy to replace banner advertisements, and also to make it easy to randomize the selection of banners on each page view.

There are several prepackaged ad servers available. These ad servers have varying capabilities but generally are meant to handle advertising campaigns. Some are very comprehensive and offer professional level capabilities. However after looking at them I was not satisfied and still wanted a way to rotate and manage the affiliate banners I have access to.

The following script is a dead simple banner advertising rotation system which is what's currently being used at http://reiki.7gen.com/ -- further, I've written a version of this as a Drupal module and expect to test it one of these days. Apparently in 2005 I'd written an article about doing this with the WordPress RandomFile extension, but that article has been lost.

This script is extremely simple. One only configures the $adsdir variable to have the path of a directory which contains banners. Each file is to contain one banner, and the contents of the file should be the banner HTML code given to you. The script simply reads all the files, chooses one (or more) banners at random, and prints it out.

You control the number of banners shown by the number of calls to doadvert.

// Dead simple banner advertisement rotation script
// - David Herron - davidherron.com
//
// Dead simple banner advertisement rotation script is free software;
// you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Dead simple banner advertisement rotation script is distributed
// in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Dead simple banner advertisement rotation script; if not,
// write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
 
$adsdir = "/path/to/advertisements/directory";
 
if (is_dir($adsdir)) {
$files = array();
if ($dh = opendir($adsdir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($adsdir.'/'.$file)) {
$files[] = $file;
}
}
closedir($dh);
 
if (count($files) >= 1) {
$which = array_rand($files, 1);
doadvert($adsdir, $files[$which]);
$which = array_rand($files, 1);
doadvert($adsdir, $files[$which]);
$which = array_rand($files, 1);
doadvert($adsdir, $files[$which]);
}
}
}
 
function doadvert($adsdir, $advert) {
$handle = fopen($adsdir.'/'.$advert, "r");
if ($handle) {
$contents = fread($handle, filesize($adsdir.'/'.$advert));
fclose($handle);
echo $contents;
}
}
 
?>

The next step is to insert the following code on your web pages:

The iframe is what causes adverts.php to be run. Whatever is output by adverts.php will appear in your page at the location of the iframe.

One must take some care with the page layout and geometry of the advertisements.

What I mean is.. say your page layout has sidebars along the sides of the page. Sidebars are often tall vertically, narrow horizontally, and would work well with a 'skyscraper' style banner. However if your banner is a horizontal banner (wide and short) then it will not fit into a sidebar, right?

Therefore all the banners you choose must have similar geometry in order to work with your page layout.

Suppose you want to display both horizontal and skyscraper banners? Obviously you won't display both into the same page region, but your page layout can have two locations for banners. One would be horizontal such as just below the title strip at the top, and the other would be a skyscraper shaped area in the sidebars. You could then make two copies of the above script, say adverts-horizontal.php and adverts-vertical.php and configure each copy for a different banner directory and then make sure to only put vertical banners in the vertical directory, etc.

Enjoy.