Webcounter


This is an extremely simple webcounter. Webcounters must produce a gif (I sure?). This script uses a program I have called makegifnum, great name that. This program take five arguments, the number you want to make into a gif, the pathname of the gif and rgb values for the foreground color(spelling? Why fight it!).

So how does the script work? The user creates a file in the same directory as the url that he wants to have a counter for. This file is called .html.counter. In it initially will be the value 0. This file must have the access rights 775 or x7x (see 'man chmod') and be group owned by wwwnull (see 'man chgrp'). If the rights are not set up properly or the file does not exist then a gif "Wrong Rights" is displayed on the page instead of the counter.


Unfortunately mere mortals can't chgrp, ah well.

Finally, the user put some HTML similar to this on the actual page to have the counter:

<H4>Visitor Number : <IMG SRC="http://www.cms.dmu.ac.uk/cgi-bin/webcounter?url=/~williams/index.html&red=255&green=255&blue=0"></H4>

The values of red, green and blue can be changed to make the gif look more sexy(?).

Here is my script. (It was knocked up quite quickly):

#! /bin/ksh

cat <<!
Content-type: image/gif
Last-modified: `date`

!

STUFF=`echo $QUERY_STRING | sed -e '
 1,$ s@%2F@/@g
 1,$ s@%7E@~@g
'`

OLDIFS=$IFS
IFS="=&"

set $STUFF

if [ $# -ne 8 ]
then
  exit 1
fi

IFS=$OLDIFS

URL=$2
RED=$4
GREEN=$6
BLUE=$8

tmp=${URL#*/}
user=${tmp%%/*}
base=${tmp#*${user}}
userpath=`eval "echo ${user}"`
HTMLPATH=${userpath}/public_html/${base}

if [ ! -f "${HTMLPATH}.counter" -o ! -r "${HTMLPATH}.counter" -o ! -w "${HTMLPATH}.counter" ]
then
  cat ~williams/public_html/cgi-bin/wrongrights.gif
  exit 1
fi

old=$(<${HTMLPATH}.counter)
((new=old+1))
echo $new > ${HTMLPATH}.counter

randfile=/tmp/counter.${RANDOM}.gif

~williams/public_html/cgi-bin/makegifnum $new $randfile $RED $GREEN $BLUE

cat $randfile

rm $randfile



[FastTrack] [BSc SE] [GTi] [AUG]

Use this address for mail: williams@dmu.ac.uk