Creating a file based AD rotator script
In this tutorial I will show you hot to create a simple AD rotator script without using database. The script can display both text and image ads and you can control the number of ads to be displayed at once.
Step 1.
The concept is that we collects all available ads in on place and randomly displays them on our web pages. Besides this the script can display textual ad block with more ads or a single random banner. All is up to you. To avoid databases we will use a simple flat file to store ad information. To make it easy to use we will create a PHP function which is relevant for the ad displaying.
Step 2.
To store AD information let's create a simple text file with name adlist.txt. Each AD has its own HTML code which we will insert on our pages. It looks like this:
Demo Text Link
The first row represents a banner ad example an the second one is a simple text ad. It is important that you write each ad in a new line as later we will interpret each line as a separate AD code. Let's fill this file with your ads and go to the implementation.
Step 3.
Now it's time to create an AD handler function let's called it to showRandomAD(). This function will be relevant for reading ads information from our text file and select one or more from it randomly to display.
So the first task in the function is to open our adlist.txt file and read it's content into an array. By using the PHP built in function file() all ad entry will be put in a separate array element. As result if your adlist.txt file contains 6 ads than you got an array with 6 items. Something like this:
< ?php
// Loading the ad list into an array
$adList = file('adlist.txt');
// Check the total number of ads
$numberOfADs = sizeof($adList);
?>
As next step we will initialize the random generator to select an item from the array:
< ?php
// Initialize the random generator
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
?>
It can happen that you want to display a text link block with more ads in it, or small partner logos and so on. So it makes sense to select and display more entries from your ad array. We can do it easy by using a loop and add a parameter to our function which declares how many ads we want to display on the page.
The complete code is this:
< ?php
// Function to display random ads from a list
function showRandomAD($ADnumber = 1){
// Loading the ad list into an array
$adList = file('adlist.txt');
// Check the total number of ads
$numberOfADs = sizeof($adList);
// Initialize the random generator
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
// Initialize the ads counter
$adCount = 0;
// Loop to display the requeste number of ads
while ($adCount++ < $ADnumber) {
// Generate random ad id
$actAD = rand(0, $numberOfADs-1);
// Display the above generated ad from the array
echo $adList[$actAD].'
';
}
}
?>
So with it we are almost ready.
Step 4.
Now let's create a sample page to demonstrate how to use our ad rotator function. You just need to include the above created Php file into your site at the beginning and you can put a single line where you want to display the ads.
< ?php showRandomAD(3); ?>
As you can see it is quite easy. Each time you want to change your advertiser you only need to edit the adlist.txt file.
The complete example code looks like this:
< ?php
require_once('microADRotator.php');
?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "DTD/xhtml1-transitional.dtd">
This is an AD rotator example
Below 3 ads will be displayed
< ?php showRandomAD(3); ?>
You can see 3 ads on this page
--
If you like this post please post your comment