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:
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.
-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.txtIt 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.logEach file has an important role in the manager:
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/ - " " #4To 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.txtIn 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