the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

Sponsored by El Scripto

Visit the Mortgage Loan Place for Home Loans and also click here to find VA Loans on our site.

Get more website traffic

home / articles / cgi

Advertising Banner Manager

Everyone is familiar with advertising banners that appear on many web pages. In fact, there's probably one on top of this page. An interesting (or annoying) aspect of advertising is the need to display different ads and then track who clicks on them. This week's article will present two small Perl scripts that will let you add dynamic content to your web site. Although I'll mention advertising here, there are other ways to use these scripts. The structure used in this ad manager allows for flexibility and easy customization.

The manager works in two separate parts. The first script, picker.pl will select a random ad and insert it into an .html file when a user asks for it. To do this, you'll need to enable Server-Side Includes (SSI) in your site's web server. To use the ad manager script picker.pl, put the code like the following into an .html file where you want an ad to appear:

 <!--#exec cmd="/home/web/bin/picker.pl"-->
When picker.pl is executed, it will randomly select an advertisement and return HTML code that may look like the following:

<a href="/cgi-bin/clicker.pl?bill">
 <img src="/adimages/bill.gif" 
   alt="Here comes Bill!" border=2><br>
  Click above on Bill
</a>

Then, the user will see something like the following:

Here comes Bill!
Click above on Bill

Notice that the code above links to clicker.pl stored in the cgi-bin directory. This is the second part of the ad manager. This CGI script keeps track of who (which IP address) clicks on the link and then promptly redirects the user to the real destination URL.

Banner Directories

The banner management system uses a set of directories, with one advertisement in each one. The picker.pl script will randomly select a directory and use its contents to generate the advertisement. Each directory has four files with the following permissions and names:
-rw-rw-rw-   1 web    71 May 12 23:00 click.log
-rw-rw-rw-   1 web    39 May 12 23:00 impression.log
-rw-r--r--   1 web   225 May 12 23:00 include.html
-rw-r--r--   1 web    26 May 12 23:00 redirect.txt
It is important to set the permissions on click.log and impression.log. They must be writable by the web server, so the easiest but least secure way of doing this is to type the following:
  chmod 0666 click.log impression.log
Each file has an important role in the manager:
  • click.log - records the time and IP address of every user who clicks on this banner advertisement.
  • impression.log - records time and IP address of every user who sees the banner.
  • include.html - the HTML code that is included in the .html file. This file allows you to generate fancy advertisements, including ones that combine text, images, forms and JavaScript.
  • redirect.txt - contains the new URL where the user should be redirected after clicking on the ad.

Banner Software

To retrieve the software, click on one of the following links. The best choice is the .tar archive, because it will automatically set the permissions on files.
  • adman.tar - complete manager in TAR archive format, best for Unix
  • adman.zip - complete manager in ZIP archive format, for those without access to tar.
Upon unpacking the archive, you'll find the following directory structure:
adman/               - main directory with PERL scripts
adman/adimages/      - where images for ads are stored
adman/ads/           - where the ads are stored
adman/ads/bill/      - sample advertisement #1
adman/ads/burn/      -   "          "       #2
adman/ads/different/ -   "          "       #3
adman/ads/thecar/    -   "          "       #4
To install the software, first copy adman/clicker.pl into the cgi-bin directory. Next move the directory adimages so that it can be served on your web server and visible to web browsers as:
   http://yourserver/adimages/
All the sample .gif and .jpg images will be in that directory. You are, of course, free to change that directory as you see fit. It's only necessary if you want to run the four demo advertisements.

Now, edit clicker.pl and picker.pl to change the value for $ADHOME:

   $ADHOME="/path/ads";
Change the path to point to the true location of the ads directory from the uncompressed archive.

Finally, find an .html file and add a Server-Side Include (SSI) to run the picker.pl script:

 <!--#exec cmd="/path/picker.pl"-->

To create a new advertisement, you can follow these steps, which are presented in the form of a shell script named mkad:

Usage: mkad  new_ad_name

#!/bin/sh
# change the following path
cd /path/ads
mkdir $1
cd $1
touch click.log impression.log
chmod 0666 click.log impression.log
echo <<EOT > include.html

<a href="/cgi-bin/picker.pl?$1">Click Me!
EOT
echo "http://redirect-url-here" > redirect.txt
In the include.html file, make sure that the word after picker.pl? matches the name of the directory where the ad is stored.

This is only the tip of the iceberg. Take a look at the sample ads included in the distribution. Then, after acquiring advertisers, you'll want to tell them how their ads are performing. In the future, I'll present some utilities to analyze the log files and generate real-time updates.

Server-Side Includes (SSI)

Server-side includes instruct your web server to parse .html files before sending the output to the client's web browser. To learn about SSI in Apache, check out these references:
Are you looking for a robust, distributed file system? Try CODA. It's free and offers failure resilience, performance, stability, security and mobile computing.

Author: Doug Steinwand
Date: [05/19/98]

More articles about CGI
More articles by Doug Steinwand
Author Biography

Open software to automate your online business
write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.