4 # A simple counter program which maintains counts for
5 # all files in a single counts file. Some systems may
6 # not allow "." files to be created, so you should
7 # rename the count file.
12 # get name of accessed document
14 $doc = $ENV{'DOCUMENT_URI'};
15 $host= $ENV{'REMOTE_HOST'};
16 $path= $ENV{'PATH_INFO'};
17 $server= $ENV{'SERVER_NAME'};
20 # open counts file for read & write
21 # get an exclusive lock on it
23 open(COUNTS, "+< /usr/lib/cgi-bin/hit_counts") || die("error: can't open hit_counts.\n");
24 flock(COUNTS, 2); # will wait for lock
27 # read counts database into associative array
31 ($count, $file) = split(/:/, $_);
32 $counts{$file} = $count;
35 # strip off silly www hostname bits.
36 if ($server =~ /^www\./) {
37 $server =~ s/^www\.//;
40 # translate some domains into other domains to avoid maintaining multiple
41 # hit count lists for the same sites that have different names.
42 if ($server =~ /^gruntose\.org/) {
43 $server =~ s/^gruntose\.org/gruntose.com/;
45 if ($server =~ /^gruntose\.net/) {
46 $server =~ s/^gruntose\.net/gruntose.com/;
48 if ($server =~ /^cromp\.net/) {
49 $server =~ s/^cromp\.net/cromp.org/;
51 if ($server =~ /^hoople\.net/) {
52 $server =~ s/^hoople\.net/hoople.org/;
56 # increment count of hit document, but not if I'm the
60 #we could do without that line probably.
63 # increment count for this file
65 $counts{$server.$doc}++;
69 # put file marker back at beginning
73 foreach $file (keys %counts) {
74 print COUNTS $counts{$file}, ":", $file, "\n";
80 # print count string to STDOUT
82 print "Content-type: text/plain\n\n";
84 print $counts{$server.$doc};