Heres my sample code for adding a count label on a webpage, graphics are inlcuded below.
This code will count visitors, but only new visitors each day get added to the overall count.
Return IP's on the same day do not get recounted. Please keep in mind that this is only
a code example, and while it does work on my site, there is no warranty implied, nor do I endorse
this code as being fit for merchantability for any purpose.
USE THIS CODE AT
YOUR OWN RISK!
counter.pl
#!/usr/bin/perl -w
#
# Counter to used for web visits
# amonotod
# 26 Aug 1999
# Tested on Netscape Ent 3.6/ WindowsNT 4.0/ActivePerl 5.005_03 Build 515 with a server cgi-bin
# Not tested on any other system yet, but should work after you change the directories
# Implementation on my system: <!--exec cgi="/cgi-bin/counter.pl?ft=1&df=test"-->
## Vars ##
$current_dir = "\/www\/cgi_bin\/";#System path to cgi-bin dir
$log_dir = "$current_dir"."count_logs\/";#My log dir for counting
$base_dir = "\/www\/docs\/"; # Sytem path to web root
$user_dir = "$FORM{d}";#the sub-url of the page to be counted
$gif_dir = "counter_gifs\/";#where I put my .jpg's, as referenced by the webroot
@gifs = ("0.jpg","1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg");#Uhm, the .jpg's
## Main ##
&html_content;
&ip_file_list;
#&dump_scr; # This is something that needs work, run this instead of the rest
# if the query string is empty or improper, ?ft=something&df=something,
# if either is blank, just dump an empty page
&count;
&display_graphics;
&show_count;
exit;
sub count{
open(COUNT,"$count_file");
$xv = (-M COUNT);
if ($xv <= $xx){ $zy = 1;}
else { $zy = 2;}
$count = (<COUNT>);
close (COUNT);
$count = $count + 0;
if ($zz == 2) { # IP file is not todays, lets zap it!
unlink $ip_file;
$count_err = 5;
}
if ($aaa == 0) { # new ip visitor, add it to the file
++$count;
open(IPFILE, ">>$ip_file");
print IPFILE "%%$ENV{REMOTE_ADDR}";
close(IPFILE);
open (COUNT, "> $count_file");
print COUNT "$count \n";
close(COUNT);
}
}
sub ip_file_list{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdt) = localtime(time);
$xx = (($sec + $min*60 + $hour*60*60)/(24*60*60));# get comparitive number
$df = "$FORM{df}";
$ft = "$FORM{ft}";
$count_name = "$df\_$ft";
$count_file = $log_dir.$count_name.".txt";
$ip_file = $log_dir.$count_name.".log";
open(IPFILE, "$ip_file");
$xy = (-M IPFILE);
if ($xy <= $xx){ $zz = 1;}#shows that ipfile is from today
else { $zz = 2;}#shows that ipfile is not from today
if ($zz == 1){
$ip_list = (<IPFILE>); $zzz = 0;
@ip_list = split (/%%/,$ip_list);
foreach $this_ip (@ip_list){
if ($ENV{REMOTE_ADDR} eq $this_ip){ $aaa = 1; $zzz = 1;}#shows that we have been here today
else { $aaa = 0;}# shows we have not been here today
}
}
close(IPFILE);
}