blog
HOME · CREATIVE · WEB · TECH · BLOG

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:

<?php
$n = rand(1,2);
if ($_SERVER['SCRIPT_NAME']=="/tags/rays-great-popcorn")
{
echo "<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41" onclick="javascript:pageTracker._trackPageview('/outbound/rays-great-popcorn');" target="rgpc"><img border="0" src="http://www.your-domain.com/resources/bnrs/rays-great-popcorn-600-200.jpg" alt="Ray's Great Popcorn" width="600" height="200"></a>";
}
elseif ($n==1)
{
echo "<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41" onclick="javascript:pageTracker._trackPageview('/outbound/rays-great-popcorn');" target="rgpc"><img src="http://www.your-domain.com/resources/bnrs/rays-great-popcorn-600-125.jpg" alt="Ray's Great Popcorn" width="600" height="125"
border="0"></a>";
}
elseif ($n==2)
{
echo "<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41&param1=".$_GET['param1']."" onclick="javascript:pageTracker._trackPageview('/outbound/freds-great-popcorn');" target="fgpc"><img src="http://www.your-domain.com/resources/bnrs/freds-great-popcorn-600-125.jpg" alt="Fred's Great Popcorn" width="600" height="125"
border="0"></a>";
}
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 was with echo, which meant all the double quotes in the HTML had to be escaped. You could alternately write the code with multiple PHP statements which may be easier for some people to understand since the HTML is just as it would be in a regular HTML document. This approach also means there's a certain amount of editing you can do in the design mode of Dreamweaver.

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 the outbound click as a page view in your analytics so you can analyze what pages do the best in getting people to click on your ads. [You can also set up a goal so all outbound clicks are seen as "conversions", but that's a topic for another post.]

The script would look like this if you wrote it with multiple statements:

<?php
$n = rand(1,2);
if ($_SERVER['SCRIPT_NAME']=="/tags/rays-great-popcorn")
{ ?>
<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41" onclick="javascript:pageTracker._trackPageview('/outbound/rays-great-popcorn');" target="rgpc"><img border="0" src="http://www.your-domain.com/resources/bnrs/rays-great-popcorn-600-200.jpg" alt="Ray's Great Popcorn" width="600" height="200"></a>
<?php }
elseif ($n==1)
{
?>
<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41" onclick="javascript:pageTracker._trackPageview('/outbound/rays-great-popcorn');" target="rgpc"><img src="http://www.your-domain.com/resources/bnrs/rays-great-popcorn-600-125.jpg" alt="Ray's Great Popcorn" width="600" height="125" border="0"></a>
<?php }
elseif ($n==2)
{
?>
<a href="http://www.your-domain.com/scripts/ad-links.php?adid=41&param1="<?php echo $_GET['param1']; ?>" onclick="javascript:pageTracker._trackPageview('/outbound/freds-great-popcorn');" target="fgpc"><img src="http://www.your-domain.com/resources/bnrs/freds-great-popcorn-600-125.jpg" alt="Fred's Great Popcorn" width="600" height="125" border="0"></a>
<?php }
else{ }
?>

But notice in the last case we had to insert a PHP echo inside the HTML to be able to use param1, so no matter how you write it, there are things that are easier and other things that may be more difficult.

Now, the way either one of those scripts is implemented is to save the script in a 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

Leave a Reply

HOME · CREATIVE · WEB · TECH · BLOG