I've written one in Perl if that's any use - the principle could quite easily be converted to PHP.
It records each hit in a CSV file, the name of which is based on an ID you choose plus the month and year (so a new file is generated each month). It records the user's IP, user agent, referer and screen res.
It uses Javascript to catch the Referer because my ISP blocks the HTTP_REFERER variable.
Javascript file "hitScript.js":
Code: Select all
// JavaScript Document
function countMe(myID1) {
var x = screen.width; var y = screen.height;
var screenRes = x + 'x' + y;
if (parent.location.href == self.location.href) {
myReferrer = document.referrer;
} else {
myReferrer = top.document.referrer;
}
var query = myReferrer.indexOf('?');
if (query > 0) {
myReferrer = myReferrer.substring(0,query);
}
if (myReferrer == '') {
myReferrer = 'noreferrer';
}
document.write('<img src="http://YOUR_PATH_HERE/hitCounter.pl?id=' + myID1 + '&referrer=' + myReferrer + '&screen=' + screenRes + '" alt="">');
return true;
}
function setCountCookie(myID2) {
var expires = new Date();
expires.setTime (expires.getTime() + 24*60*60*1000);
expiresGMT = expires.toGMTString();
cookieID = myID2;
document.cookie = "id=" + cookieID + "; expires=" + expiresGMT + "; path=/";
return true;
}
function checkCountCookie(myID3) {
var cookieFound = false;
var cookieString = document.cookie;
var i = 0;
while (i <= cookieString.length) {
start = i;
end = start + myID3.length;
if (cookieString.substring(start,end) == myID3) {
cookieFound = true;
break;
}
i++;
}
return cookieFound;
}
cookieFound = checkCountCookie(localID);
if (cookieFound != true) {
countMe(localID);
setCountCookie(localID);
}
Perl file "hitCounter.pl":
Code: Select all
#!/usr/bin/perl
BEGIN
{
use CGI;
use CGI::Carp qw(carpout fatalsToBrowser set_message) ;
set_message('<p>An error has occured with this script.</p>') ;
}
$qs = $ENV{'QUERY_STRING'};
@qs = split(/\&/,$qs);
@qs0 = split(/=/,@qsї0]);
@qs1 = split(/=/,@qsї1]);
@qs2 = split(/=/,@qsї2]);
$id = @qs0ї1];
$referrer = @qs1ї1];
$screen = @qs2ї1];
($sec,$min,$hr,$day,$mon,$yr,$weekday,$doy,$isDST) = localtime(time);
$mon = $mon + 1;
$year = $yr + 1900;
$data_file = $id . '_' . $mon . $year .'_hits.csv';
$hits_file = $id . '_' . $mon . $year .'_hits.txt';
$path = $ENV{'DOCUMENT_ROOT'} . '/YOUR_RELATIVE_PATH/logs';
$data_file = $path . '/' . $data_file;
$hits_file = $path . '/' . $hits_file;
$image = $ENV{'DOCUMENT_ROOT'} . '/YOUR_RELATIVE_PATH/blank.gif';
$remote_ip = $ENV{'REMOTE_ADDR'};
open(DATA_F, ">>$data_file") || die "Could not open DATA file $data_file: $!";
printf DATA_F ('%04d-%02d-%02d,',$year,$mon,$day);
printf DATA_F ('%02d:%02d,',$hr,$min);
print DATA_F "$remote_ip,$ENV{'HTTP_USER_AGENT'},$referrer,$screen\n";
print "Content-type: image/gif\n\n";
open (IMAGE, "$image") || die "Could not open image: $!";
binmode IMAGE;
binmode STDOUT;
print (<IMAGE>);
You need to create a subfolder in the directory you place the scripts called "logs" where the logs will be stored. You need to place a blank image called "blank.gif" in the script directory. Then add the following code into each page of your site:
Code: Select all
<script language="JavaScript" type="text/JavaScript">
<!--
var localID = 'YOUR_ID';
-->
</script>
<script language="JavaScript" src="http://YOUR_PATH_HERE/hitScript.js"></script>
<noscript>
<img src="http://YOUR_PATH_HERE/hitCounter.pl?id=YOUR_ID&referer=noscript" alt="">
</noscript>
Any comments or improvements gratefully received!