Error message

Deprecated function: Methods with the same name as their class will not be constructors in a future version of PHP; GeSHi has a deprecated constructor in require_once() (line 915 of /var/www/

Dead simple banner advertisement rotation script

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

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

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.