The following are simple instructions to create a script to randomly rotate banner, text and interstitial ads on your site. You can rotate anything else you want as well, really, but the main idea is to use it on ads. This is generally meant for PHP enabled sites but there is also code for regular HTML as well, using JavaScript.
In the following tutorial I’ll be refering to the ad code as objects. This is because it doesn’t have to be just ads. You can use this rotation script to rotate plain text, for example to show quotes randomly on your front page. It can also be used to load JavaScript code for interstitial ads or anything else. I haven’t tested it but you can probably have it rotate text in your title by putting it between your TITLE tags.
Script Configuration
The first thing you need to do is create a new file named adverts.php. Within you will add:
|
1 |
<?php $bannerCounter= 1; |
Each object is written in, after the first line above, as shown below:
|
1 2 |
$bannerCode[$bannerCounter] = "Replace this with ad code or text (object)";
$bannerCounter++; |
Now a generic example of a three (3) banner rotation. Note that every instance of a double quote (") in your object needs to be escaped with a backslash (\) in front of the double quote (") in order to look like \". This is because the double quotes that open and close the script will get confused if your double quotes aren’t escaped.
|
1 2 3 4 5 6 7 8 9 10 11 |
$bannerCode[$bannerCounter] = "<a href=\"http://www.dorks.com/\"><img alt=\"Meet some funny dorks\" src=\"funny.dorks.gif\" style=\"border:0;\" /></a>";
$bannerCounter++;
$bannerCode[$bannerCounter] = "<a href=\"http://www.lol.com/nuts\" target=\"_top\"><img alt=\"Laugh your ass off\" src=\"http://www.lol.com/banners/nuts.gif\" style=\"border:0;\"></a>";
$bannerCounter++;
$bannerCode[$bannerCounter] = "<a href=\"http://www.lol.com/beans\" target=\"_blank\">
<img alt=\"Laugh your ass off\" src=\"http://www.lol.com/banners/beans.gif\"
width=\"395\" height=\"141\" style=\"border:0;\">
</a>";
$bannerCounter++; |
The next piece of code comes after all of your objects have been added, like in the example above. What this does is randomly choose one of your objects to display after it checks to see that you have one or more objects to choose from.
|
1 |
$bannerAdTotals = $bannerCounter - 1; if($bannerAdTotals>1) { mt_srand((double)microtime() * 1234567); $bannerPicked = mt_rand(1, $bannerAdTotals); } else { $bannerPicked = 1; } $bannerAd = $bannerCode[$bannerPicked]; ?> |
That’s it for the adverts.php file.
Note: If you are going to be using the JavaScript method there is a line that needs to be added to the bottom of this script’s configuration as well as a possible change in your objects, see below for the JavaScript method.
PHP Code
Your PHP pages need to include the adverts.php script first, then show the objects. You can put this code where ever you want the objects to appear. For example, at the bottom of your page, or in a space you already chose for an ad box. The best place for interstitial ads is just before your closing BODY tag. Following is the code you need to do this, remember to change the path to where your script is located:
|
1 |
<?php include_once("/path/to/adverts.php"); echo "<div>$bannerAd</div>"; ?> |
You can configure the DIV tag to make it look however you want (or remove it completely, especially if it’s just JavaScript for an interstitial). The $bannerAd value is the object itself being passed into your PHP file.
JavaScript Method & Code
The first thing you need to do for the JavaScript method is to change all of the $bannerCode[$bannerCounter] sections in the adverts.php file into a single line with no breaks. All this means is to remove every line break between $bannerCode[$bannerCounter]=" and the closing ". In the three ad example above the last ad has line breaks, this is what it must look like to work with the JavaScript method:
|
1 2 |
$bannerCode[$bannerCounter] = "<a href=\"http://www.lol.com/beans\" target=\"_blank\"><img alt=\"Laugh your ass off\" src=\"http://www.lol.com/banners/beans.gif\" width=\"395\" height=\"141\" style=\"border:0;\"></a>";
$bannerCounter++; |
The next change is the line that needs to be added to the bottom of adverts.php:
|
1 |
$bannerAd = str_replace('"', '\'', $bannerAd); print("document.write(\"$bannerAd\")"); |
This just changes some double quotes into escaped single quotes.
Now we can add the code into the HTML files.
In the PHP files we used:
|
1 |
<?php include_once("/path/to/adverts.php"); echo "<div>$bannerAd</div>"; ?> |
For the JavaScript we will use:
|
1 |
<script language="text/javascript" src="/path/to/adverts.php"></script> |
You can also use the full URL to the adverts.php file if you want to serve the same file to several domains.
Note: If your ads are blank or not loading then it’s more than likely that you still have some line breaks in the ad code. Check back to see if you’ve removed them all.
Related posts:
- XHTML Strict & target=”_blank” (and _top) I had made a post a few days ago regarding...
- Valid XHTML Vertical Centering Using CSS The simplest method to vertically center your elements while using...
- Embedding Flash Files With Valid XHTML (Strict) I’m sure a lot of people want to know the...
- Generate an OpenSSL CSR for Apache2 and ModSSL Generating an encrypted RSA key You can skip this step...
- Reduce the Microsoft Windows 7 WinSxS Folder’s Size You can reduce the Microsoft Windows 7 WinSxS folder’s size...

0 Comments.