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

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

Windows Text to Speech in WSH JavaScript

BSave and BLoad for the Commodore 64

Open Source Licenses

My Big Shareware Splash

Charging by the Byte

Blog Posts by Category

A Simple Parser for a Small Command Line Interface

Screen Capture from Multiple Monitors in Java

WSH2EXE part 1

Java in a Windows EXE with launch4j


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer