#!/usr/bin/perl # uvhitctr2.pl - counter program for HTML pages # - by Owen Townsend, July 2005 # - shows hitctrs by month for thisyr & lastyr # - internet server must allow SSI's (Server Side Includes) in '.htm's # - hitctr filename made from docname_YYYY & stored in subdir cgi-bin/ctrs # - eg: ctr file in 2005 for index.htm would be cgi-bin/ctrs/index.htm_2005 # # ------------------ sample coding in HTML document --------------------- # #

Visitor Counters for ThisYear & LastYear

# # # ----- generates following ----- # # ** Visitor Counters for ThisYear & LastYear ** # # ThisYear=000016 (J=0,F=0,M=0,A=0,M=0,J=0,J=16,A=0,S=0,O=0,N=0,D=0) # LastYear=000078 (J=1,F=2,M=3,A=4,M=5,J=6,J=7,A=8,S=9,O=10,N=11,D=12) # # ------------------ free program from UV Software -------------------- # # You may download this program from the UV Software web site # ---> www.uvsoftware.ca/htmljobs.htm#Part_3 <--- # # Note - some ISP's only enabled SSI's in HTML files with suffix '.shtml' # - on request they should be able to enable in '.htm' files as well # # get year,month,day,date for use as part of ctr filenames @dates = getdate(); $year1 = $dates[0]; $month = $dates[1]; $month0 = ($month -1); $year2 = ($year1 -1); # # create ctr filenames for thisyear & lastyear # - use DOCUMENT_URI as ctr filename (within subdir ctrs) # - extract right hand segment based on '/' delimiters # - append current year & lastyear $docurl = $ENV{"DOCUMENT_URI"}; $docuri = rindex($docurl,"/"); $docfile = substr($docurl,$docuri+1,30); $ctr1name = sprintf("%s_%s",$docfile,$year1); $ctr2name = sprintf("%s_%s",$docfile,$year2); if ($ctr1name eq "") { $ctr1name = "ctrname1ERR"; } if ($ctr2name eq "") { $ctr2name = "ctrname2ERR"; } $ctr1file = "ctrs/$ctr1name"; $ctr2file = "ctrs/$ctr2name"; # # write HTTP hdr block info required before any print output # - either the counter value or error messages print "content-type: text/html\n\n"; # #*eject # open thisyear ctr1file & read current values # if we cant open (1st time called) - write initial value of zeros unless (open(THISYR,"$ctr1file")) # open ctr1file for input { open(THISYR,">$ctr1file"); # if failure - open for output print(THISYR "0|0|0|0|0|0|0|0|0|0|0|0|\n"); close(THISYR); # - close open(THISYR,"$ctr1file"); # - reopen for reading print "$ctr1file open input err, open output,write zeros,close,reopen
"; } # $ctr1s = ; # read thisyr ctrvalues @ctr1a = split(/\|/,$ctr1s); # split string via '|' seps into array $ctr1a[$month0]++; # increment this month's ctr # # sum thisyr 12 month ctr to get total for current year $thisyr = 0; for ($ii=0; $ii < 12; $ii++) { $thisyr += $ctr1a[$ii]; } # $ctr1s = join("\|",@ctr1a,""); # recreate array string for write # # reopen file for writing & write new value unless (open(THISYR,">$ctr1file")) { die "cannot open thisyr ctrfile $ctr1file to write new ctrvalues"; } flock(THISYR,2); # lock file before write print(THISYR $ctr1s); # write new value flock(THISYR,8); # unlock file close(THISYR); # close ctr1file # # format thisyr output string with month ID's $ctr1sp = sprintf( "ThisYear=%06d   (J=%d,F=%d,M=%d,A=%d,M=%d,J=%d,J=%d,A=%d,S=%d,O=%d,N=%d,D=%d)" ,$thisyr,$ctr1a[0],$ctr1a[1],$ctr1a[2],$ctr1a[3],$ctr1a[4],$ctr1a[5],$ctr1a[6] ,$ctr1a[7],$ctr1a[8],$ctr1a[9],$ctr1a[10],$ctr1a[11],$ctr1a[12]); # #*eject # open lastyear ctr2file & read lastyear values # if we cant open - just store zero values unless (open(LASTYR,"$ctr2file")) # open ctr1file for input { $ctr2s = "0|0|0|0|0|0|0|0|0|0|0|0|\n"; } else { $ctr2s = ; # read lastyr ctrvalues } # @ctr2a = split(/\|/,$ctr2s); # split lastyr string via '|' seps into array # # sum lastyr 12 month ctr to get total for last year $lastyr = 0; for ($ii=0; $ii < 12; $ii++) { $lastyr += $ctr2a[$ii]; } # # format thisyr output string with month ID's $ctr2sp = sprintf( "LastYear=%06d   (J=%d,F=%d,M=%d,A=%d,M=%d,J=%d,J=%d,A=%d,S=%d,O=%d,N=%d,D=%d)" ,$lastyr,$ctr2a[0],$ctr2a[1],$ctr2a[2],$ctr2a[3],$ctr2a[4],$ctr2a[5],$ctr2a[6] ,$ctr2a[7],$ctr2a[8],$ctr2a[9],$ctr2a[10],$ctr2a[11],$ctr2a[12]); # print "$ctr1sp
\n$ctr2sp\n"; # output counter values # exit(0); # exit program # #------------------------------ subroutines ---------------------------- # getdate - get current date formatted as YYYY-MM-DD # sub getdate { local ($ltSS,$ltMM,$ltHH,$ltdd,$ltmm,$ltyy,$ltwd,$ltyd,$ltis); local ($year,$month,$day,$today); ($ltSS,$ltMM,$ltHH,$ltdd,$ltmm,$ltyy,$ltwd,$ltyd,$ltis) = localtime; $year = $ltyy + 1900; $month = $ltmm + 1; $day = $ltdd; $date = sprintf("%04s-%02s-%02s",$year,$month,$day); return($year,$month,$day,$date); } #---------------------------- end of uvhitctr2.pl -------------------------