code

counter

I think I succeeded in reverse engineering those free hit counters/referrer loggers/webstat thingamajiggers. No pretty graphs or useful statistics, but you'll get the raw info....

Of course, this might not be the most useful thing to have done, unless you're like me, and have access to CGI scripts, but not to the web server logs.

server side: the CGI script

Stick this in your cgi-bin/ directory. I call it spy.pl. Call it whatever you want, just make sure you change the JavaScript down below as well.

#!/usr/local/bin/perl
#spy.pl
use CGI qw(:all);

$logfile="Path to whereever you want to store the info on your server";

$query = new CGI;
print $query->header('text/javascript');

# The following environment variables are automatically accessible
# through CGI.pm.  See the man page for more info.

$http_referer=$query->referer(); 
# HTTP_REFERER is mostly useless, but I guess it allows you to make
# sure that no one has stolen your code wholesale and is now
# inadvertantly logging their page hits to your server

$remote_addr=$query->remote_addr();
$remote_ident=$query->remote_ident();
$remote_host=$query->remote_host(); $user_agent=$query->user_agent();
$user_name=$query->user_name();

# The following parameters are set by the accompanying Javascript
$page=$query->param("page");
$document_referrer=$query->param("referrer");

# Get the timestamp
$month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]];
$mday = (localtime)[3];
if ($mday < 10) { $mday = "0" . $mday; }
$hour = (localtime)[2];
if ($hour < 10) { $hour = "0" . $hour; }
$min = (localtime)[1];
if ($min < 10) { $min = "0" . $min; }
# Output the log entry
open (LOG, ">>$logfile");
print LOG "$month $mday $hour:$min ";

print LOG "Page: $page ";
print LOG "HTTP Referrer: $http_referer ";
print LOG "document.referrer: $document_referrer ";
print LOG "Remote Address: $remote_addr ";
print LOG "Remote Identity: $remote_ident ";
print LOG "Remote Host: $remote_host ";
print LOG "User Agent: $user_agent ";
print LOG "User Name: $user_name ";
print LOG "\n";

close (LOG);

There's a lot of other variables to peek at....check out the man page for CGI.pm (the CGI Perl module)

client side: the javascript

So as not to pollute your HTML too much, you can put the following code in a separate file. I called it spy.js.

function spy(page)
{
        referrer=escape(document.referrer);
        document.write ('<script language="JavaScript"
                  src="URL to spy.pl on your CGI server?page=' + page + '&referrer=' + referrer + '">' );        
        document.write ('</script>');
}

client side: the HTML

Now, stick this on the pages that you want to monitor, anywhere between <body> and </body>

<script language="JavaScript" src="path to spy.js">
</script>

<script language="JavaScript">
<!--
  spy("some arbitrary identifier");
// -->
</script>

notes

This is all grossly unnecessary if you have real web hosting and have access to your weblogs. The Javascript kludgery is largely unnecessary if you have access to Server Side Includes. And if you want pretty pictures and/or useful statistical breakdowns, you'll need to write more CGI scripts, or just sign up for something like NedStat, which is free anyway.


Last update: 2001-04-24
code main | fato profugus