Thursday, November 13th, 2008

How To Do Rotating Banner And Text Ads With PHP

If you want to have content on your site that changes every time someone loads the page there are a number of possible solutions. Most typically these are ads - either textual or graphical "banner ads", but the same solution can be used for other random content.

There are ad packages/"scripts" out there which are decent, but you should choose them carefully since moving to anything else will be time consuming once you're fully set up (so make sure you really like the package and it's easy to maintain before investing too much energy in it). The other problem with ad packages is that they're almost always database driven and I've frequently seen problems where they don't redirect the person correctly. So check out sites that use the ad package to make sure it works in the real world.

There are two elements to handling ads - displaying the ad, and handling the click. This post will just address displaying the ad. I'll do another post on how you can handle the outbound link. The advantages of what I'm going to show here are:

  • It's something you write and maintain yourself - that means you can customize it as you see fit. It's not some scary black box you don't really understand.
  • It's not database driven - that means it will be more reliable. It also means it has limitations - if you've got a very large number of ad types (say over 1,000), you should be using a database driven solution.

The script is just a big if elseif statement. We're going to add in two features to the idea of a random banner ads - 1) static ads on certain pages, 2) special parameters for certain ads.

Here's a brief version of the script:

$n = rand(1,2);
if ($_SERVER['SCRIPT_NAME']=="/tags/rays-great-popcorn")
{ ?>
<a href="" onclick="_gaq.push(['_trackEvent', 'Rays Great Popcorn', 'Banner Ad Click To Sponsor', 'Bottom Banner - Table Pic with Popcorn', 0, false]);" target="rgpc"><img border="0" src="" alt="Ray's Great Popcorn" width="600" height="200"></a>
<script type="text/javascript"> _gaq.push(['_trackEvent', 'Rays Great Popcorn', 'Banner Ad View', 'Bottom Banner - Table Pic with Popcorn', 0, false]); </script>
elseif ($n==1)
{ ?>
<a href="" onclick="_gaq.push(['_trackEvent', 'Rays Great Popcorn', 'Banner Ad Click To Sponsor', 'Bottom Banner - 2 Bowls of Popcorn', 0, false]);" target="rgpc"><img src="" alt="Ray's Great Popcorn" width="600" height="125"
<script type="text/javascript"> _gaq.push(['_trackEvent', 'Rays Great Popcorn', 'Banner Ad View', 'Bottom Banner - 2 Bowls of Popcorn', 0, false]); </script>
<?php }
elseif ($n==2)
{ ?>
echo "<a href="<?php echo $_GET['param1']; ?>" onclick="_gaq.push(['_trackEvent', 'Freds Great Popcorn', 'Banner Ad Click To Sponsor', 'Bottom Banner - Hottest Popcorn In Town', 0, false]);" target="fgpc"><img src="" alt="Fred's Great Popcorn" width="600" height="125"
<script type="text/javascript"> _gaq.push(['_trackEvent', 'Freds Great Popcorn', 'Banner Ad View', 'Bottom Banner - Hottest Popcorn In Town', 0, false]); </script>
<?php }
else{ }

So let's go over what the script does... First, it sets a random number into the $n variable. Since we have two ads, it randomly picks a number between 1 and 2. Obviously as you add more cases for more ads, you'd up 2 to a bigger number.

Then it tests to see if we're on the page with the URL /tags/labels/rays-great-popcorn and if so, it displays a special ad that was made just for that page. That page doesn't get a random ad, it always gets that particular ad.

In the next two cases it checks the random number and randomly displays one of the two banner ads you have for your site (of course you'd have more than two).

In the second case it uses a special extra parameter in the outbound URL for the banner ad (param1). It uses PHP's $_GET array to pull the param1 element out of the array. The $_GET array holds all the query parameters you passed in the URL string when you did the include that called the script (we'll show that in a moment). That means that you have to pass all the parameters into the script that you might use in any of the random ads.

The way the script was written with multiple PHP statements. Alternatively you could write it with echo statements, but then you have to escape many of the characters. Generally multiple PHP statements makes for code which is much easier to understand and less prone to errors.

The HTML is pretty basic - an image with a link, so when you click on the image you follow the link. The one advanced aspect is the Google Analytics event tagging that I've added in the link. That registers both the view of the banner ad as well as the outbound click as an event in Google Analytics. That will let you analyze what banner ads have the best click thru ratios, etc. That's a much longer topic and one I should turn into a blog post of it's own.

The best way to implement the script above is to save it in a separate PHP file and insert it into the page with a PHP include. So let's say we named the script banner-ads.php and we put it in an /includes/ directory at the root of our site. The include would look something like this:

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/banner-ads.php?param1=forum"); ?>

So that pulls the file banner-ads.php and passes the value "forum" in as param1. That could be useful if you wanted to know the section of your site that was calling the banner ad - just change the value in each one of your templates (one value for the blog templates, one for the forum templates, etc.) But that could be achieved another way:

<?php $param1="forum"; include($_SERVER['DOCUMENT_ROOT']."/includes/banner-ads.php"); ?>

If you did that, then you wouldn't access the value with the $_GET array in the script, instead you'd just access the $param1 variable directly.

So there you have it - a simple way to implement random banner ads that's rock solid and easy to maintain by anyone with just a little bit of technical skills.

Tags: ,
Categories: Advertising, PHP


  1. Matthew Says:

    Thank you for your tutorial, this will help me greatly as I move away from plugins and keep more development in house.

  2. Shack Says:

    I am trying to use this code with three different Affiliate links but they are appearing sequentially under one another.

    Please help.

    Thanks very much.

Leave a Reply