#!/usr/local/bin/perl
#Aggemam Text Counter(tm) by Simon Skals
#
#This is version 1.2 patchlevel 3
#
#Feel free to modify this script, call it your own, or whatever.
#You can mail me at aggemam@gid.dk and be subsequently ignored.
#
#The most recent version of this script can always be found at this URL:
#Aggemam homepage http://www.gid.dk/~simon/
#
#DESCRIPTION:
#Aggemam Text Counter is a simple text counter that requires SSI. For good
#measure, it logs the IP address of the last visitor and the counter will not
#increment until it is fetched by a new IP.
#
#INSTALLATION:
#Installation should be easy. Just copy the script into your cgi-bin and
#chmod it to 755. Touch the files counter.dat and hostname.dat in whatever
#location you've chosen for them. Remember to make them readable and writable
#for the world.
#
#Finally, just stick this piece of SSI in your HTML:
# <!--#exec cgi="/cgi-bin/aggemam.pl"-->
#
#Please note that this script will suck if your system doesn't support flock.
#
#CONFIGURATION:
#Set this variable to the directory where you want to store the data files.
#Be careful not to add a final trailing slash.
$datadir="/usr/home/yourlogin/somedir";
#Ok, let's get to work.
#Prepare for HTML output.
print "Content-type: text/html\n\n";
#Get the REMOTE_HOST variable to find out who's hitting the page.
$host = $ENV{'REMOTE_HOST'};
#Open the file counter.dat to find out the current value of the counter.
open (counter, "<$datadir/counter.dat") || &error("Ouch, could not open $datadir/counter.dat.");
flock counter, 2;
$counter = <counter>;
close (counter);
#Next we've got to open the file hostname.dat to see the latest hostname to
#hit the page.
open (hostname, "<$datadir/hostname.dat") || &error("Ouch, could not open $datadir/hostname.dat.");
flock hostname, 2;
$oldhost = <hostname>;
close (hostname);
#Check whether these two hostnames are identical.
if ($host eq $oldhost) {
#If they're identical, we don't want the counter to increase. Instead we just
#print the current value that we got from counter.dat.
print "$counter";
exit;
} else {
#If they're not identical, we want to increase the counter with 1 and save this
#value to counter.dat. Then we save the new hostname to hostname.dat. Finally,
#we print the new value of the counter.
$counter++;
open (newcounter, ">$datadir/counter.dat") || &error("Ouch, could not open $datadir/counter.dat.");
flock newcounter, 2;
print newcounter "$counter";
close (newcounter);
open (newhostname, ">$datadir/hostname.dat") || &error("Ouch, could not open $datadir/hostname.dat.");
flock newhostname, 2;
print newhostname "$host";
close (newhostname);
print "$counter";
exit;
}
#Here's the subroutine we use in case something went haywire.
sub error {
$descr = $_[0] ;
print "[$descr $!.]";
exit;
}