#!/bin/ksh # SSI Counter # Copyright © 1998, 2005, 2006 by David Ross (david@rossde.com) typeset -L20 ipaddr typeset -L20 oldip typeset -R30 now typeset -i newsecs typeset -i oldtime typeset -i thousands typeset -i counter typeset -L30 environment # Get counter file name cntr_file=${HTTP_ross_cntrname}.dat # collect IP addresses of visitors ipaddr=$REMOTE_ADDR # time in seconds let newsecs=$(date -u +'%s') # Does file exist? if [ -a $cntr_file ]; then #yes read counter oldtime oldip < $cntr_file # read file else #no counter=0 # initialize counter oldip='000.000.000.000' oldtime=0 fi increment='yes' if (( $newsecs - $oldtime > 300 )); then # hit not recent let counter=$counter+1 # increment it elif [[ $ipaddr != $oldip ]]; then # from different visitor let counter=$counter+1 # increment it else increment='no' fi print $counter $newsecs $ipaddr > $cntr_file # save it to the file # Note: need new IP and time even if 'old' hit if (( $counter >= 10000 )); then # insert comma let thousands=0 while (( $counter >= 1000 )); do let thousands=$thousands+1 let counter=$counter-1000 done if (( $counter >= 100 )); then pcounter=${thousands}','${counter} elif (( $counter == 0 )); then pcounter=${thousands}',000' # insert zeros elif (( $counter < 10 )); then # insert zeros pcounter=${thousands}',00'${counter} else # insert one zero pcounter=${thousands}',0'${counter} fi else # don't insert comma pcounter=$counter fi print 'Content-type: text/html' print '' print $pcounter if [[ $increment == 'yes' ]]; then # add to log only for 'new' hit now=$(date) # print "$ipaddr | $now | $HTTP_USER_AGENT | $HTTP_REFERER" >> agent.log print "$ipaddr $now" $pcounter >> visit_${HTTP_ross_cntrname}.log fi #let environment=$(printenv REMOTE_ADDR) environment=$safe_env_lst print $environment >> env.log print "end" >> env.log