Jim Lawless' Blog


Site Tracking with Perl

Originally published on: Sun, 07 Jun 2009 22:13:34 +0000

About a month ago, I wrote a short article called PHP, Transparent GIF's, and Web Tracking that turned out to be one of the more popular entries on this blog. If you haven't already, you might wish to read that posting first as it goes into more detail on the fundamentals of site tracking using transparent GIF images.

It's not the first time I'd written up a web application that delivers a tracking GIF to the client. The earliest implementation I can remember writing was written in Perl.

In the mid-90's, I used to write a lot of custom CGI ( Common Gateway Interface ) scripts in Perl. I opted to use PHP for the earlier article because it seems to now be more ubiquitous. Back in "the day", however, Perl was everywhere. Unfortunately, it wasn't always the same version of Perl on each site, nor was Apache always configured to recognize the same file extensions as CGI's the same way, nor was the Perl interpreter always found in the same spot.

Perl was always my trusted ally, however. Once the site's configuration was understood, I found Perl to be very easy to use.

I have noted that a number of hosting sites that offer PHP also offer some flavor of Perl. So, I thought I'd provide a short sample Perl CGI script that will deliver the same single-pixel GIF as the one used in the PHP article.

tracker.pl


#!/usr/bin/perl

# License: MIT / X11
# Copyright (c) 2009 by James K. Lawless
# jimbo@radiks.net http://www.radiks.net/~jimbo
# http://www.mailsend-online.com
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

binmode(STDOUT);
print "Content-type: image/gif\n\n";
print "GIF89a" ;
print pack("CC",01,00);
print pack("CCCCCCCC",1,0,128,0,0,255,255,255);
print pack("CCCCCCCC",0,0,0,33,249,4,1,0);
print pack("CCCCCCCC",0,0,0,44,0,0,0,0);
print pack("CCCCCCCC",1,0,1,0,0,2,2,68);
print pack("CCC",1,0,59);

# logging stuff goes here...
open(LOG,">>somelogfile");
print LOG $ENV{"REMOTE_ADDR"},"\n";
print LOG $ENV{"REMOTE_HOST"},"\n";
print LOG $ENV{"HTTP_USER_AGENT"},"\n";
print LOG $ENV{"HTTP_REFERER"},"\n";
close(LOG);

I again left the actual logging as a fill-in for the reader. The code presented just writes a few CGI headers to a flat file.

I also did not implement any mechanism to convey a page identifier via a parameter as I did in the PHP article as I suspect a number of you use different approaches/libraries for parsing parameters. I used to use cgi-lib.pl because it was lightweight. I was aware that others were using different libraries and such, so I haven't placed any code to parse parameters from the querystring.

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: WSH2EXE - The Final Chapter
Next post:Generating Primes with XSLT


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Invoking the Default Windows Screen-Saver

Obfuscated C

Blogoversary

Extracting URL Addresses from Text in C

Getting the Windows Console Text Color

Thwarting HTTP Referer Trackbacks

Scott Ballantyne: Blazin' Into Forth

Choose your own Adventure with Sinatra

An Embedded Mini-Interpreter

Screen Captures with Java and Clojure


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service