Accesses since: 01/Apr/1996: 756
Most ISP's refresh their access logs periodically. Since this counter counts current accesses only, you will lose counter data if the server's access log is restarted with zero. If that doesn't matter to you, use EasyCounter I.
EasyCounter II is significantly more complex, but also much more elegant and useful. It keeps a daily and a cumulative access count and displays both (or just one, if you prefer). The page display looks like:
Page accesses on 26/Apr/1996: 126
Total accesses since July 8,1995: 38873
. This count was last updated on 26/Apr/1996
You can also make a summary html page of accesses to all pages you maintain. This is called mysite.html.
This is my third major revision to EasyCounter. The first was posted November 12, 1995. The second was posted April 27, 1996. The instructions were modified some on July 13, 1996. The third revision was posted March 11, 1998. I hope the insructions are clear, but in truth, EasyCounter II is not really easy to follow. It does work well, however, and it may give you ideas even if you choose not to implement it. I think EasyCounter I is, indeed, easy, and I hope it is for you, as well.
When you use EasyCounter, you will use a shell script that scans your system access log for references to your pages. Once you have completed the counting, you will rebuild your web page(s), placing the counter information at the end. Finally, you will place your rebuilt page in the public_html directory.
If you use EasyCounter II, it will also build a mini access log of accesses to just your html pages (EasyCounter I does not build or use the mini access log).
Once you have built the mini log, you will use it as a database for counting accesses to your html page(s). The mini log makes it easier to keep a cumulative counter. It also lets you generate pages such as mysite.html
EasyCounter II can keep track of accesses to one or more html pages (you can also count non-html pages, but you will have to make some changes to do that. I don't bother, since the only pages I am interested in counting are my html pages).
Disclaimer:There is more than one way to skin a cat, and there may better and more elegant ways to put a hit counter in your page. If you have suggestions for improvements, let me know.
I have tweaked EasyCounter so it works for me. I certainly hope it works for you as well. But, your mileage may differ. Use at your discretion, peril, etc. I have attempted to make these instructions clear enough for non-computer types such as myself to follow. Let me know if they are not.
1. Make a new directory called easycount [mkdir easycount]. The square brackets are for clarity. Do not include them when you type the commands.
2. Change to this directory, [cd easycount], and copy your web page to it. (EasyCounter can count the accesses for more than one html page, but it is easier to explain its operation assuming just one page. You would repeat this process for multiple pages.)
3. If your page (e.g. mypage.html) is in the public_html directory, the command would look like this:
cp /path/to/public_html/mypage.html mypage.html.Be sure to type your real path and real page file name.
4. Edit mypage.html by deleting the final </body> and </html> tags. (I like Pico, but any editor will do.) These tags should be the last ones on your page.
5. Save the resulting page as mypage_short.htm You will be adding the counter to the end of this web page. Use your page name instead of "mypage."
#--------------------- EasyCounter I --------------------------
# Assume you want to add a counter to example.html.
# If you haven't already done so, make a directory called easycount.
# Copy example.html to easycount directory. Edit the file to delete the
# </body></html> tags. Save the resulting file as example_short.htm.
# We will use example_short.htm to re-build example.html. Don't
# forget to change the "example" names to something that reflects
# your html page name.
#
# User defined variables. Enter needed values as indicated.
#
url="http://www.bcpl.lib.md.us/~sandyste/example.html" # Enter your
# page url
user=sandyste # Replace sandyste with your userid as it appears in
# access_log
logpath="/carl/httpd/logs/access_log" # Enter your access_log path in
# place of /carl/httpd/logs/access_log.
pubpath="/$HOME/public_html" # Enter path to your public_html
# directory in place of/$HOME/public_html if yours is different.
page1name=example.html # Replace example.html with your page name
page1=example.html # Replace example.html with your page name
cp example_short.htm $page1 # Replace "example" with your page.
#
# This ends user defined variables. You should not have to modify rest
# of script.
#
# Next section counts the accesses.
#
echo "Counting accesses to $page1 ....be patient."
grep -c $user/$page1name $logpath > "$page1"_hits
newcount=`cat "$page1"_hits`
# Adding the access count to the html page.
echo "<br>" >>$page1
echo "Accesses since: " >> $page1
head -1 $logpath | cut -d' ' -f 4 | cut -d'[' -f 2 | cut -d':' -f 1 >>$page1
echo "<b>" >>$page1
echo "$newcount" >>$page1
echo "</b>" >>$page1
echo "<address>" >>$page1
echo "Access counter by <a href=" >>$page1
echo "\"http://www.bcpl.lib.md.us/~sandyste/ezcount.html\">" >>$page1
echo "<b>EasyCounter</b></a>" >>$page1
echo "</address>" >>$page1
# The following four lines are added to the html file to complete it.
# If you don't want the page url included, comment out the next 3 lines.
echo "<hr>The URL of this page is" >>$page1
echo "$url$page1name" >>$page1
echo "<hr>" >>$page1
echo "</body></html>" >>$page1
# The completed file is copied to example.html in the public_html
# directory. Uncomment the following line when the script works to
# your satisfaction
#cp $page1 "$pubpath"
echo Finished
The file, example_short.htm contains the whole page except for the access data and the ending html tags.
The grep -c command looks for the lines that contain references to your page in the server's log. [-c] indicates we are only counting the lines. EasyCounter will work for you as long as your server's access_log has the following format.
pegasus.rutgers.edu - - [26/Jul/1995:13:52:43 -0400] "GET /~jdoe/mypage.ht ml HTTP/1.0" 200 2234This is the common log format. It is probably what you have. To take a peak at your server's log, try this command (use the correct path for your server).
more tail /path/to/httpd/logs/access_logThis should show you the last 10 accesses. If your access_log lines have a different format, EasyCounter will not work unless you modify the grep and cut lines. I believe, however, that many, if not most, access logs follow the above format.
Save the shell script as [ezI.scr] or anything else you want.
Make [ezI.scr] executable. At the unix prompt, type
chmod u+x ezI.scr
Test the script by typing [ezI.scr] at the unix prompt. Before you test, be sure
cp $page1 $pubpathis commented out. This line copies the rebuilt html page to your public_html directory. This will prevent you from messing up your html file in case EasyCounter doesn't work. After running [ezI.scr], take a look at your rebuilt page by typing [more example.html] at the unix prompt. You should see the access data at the end of the page. If everything looks OK, uncomment
cp $page1 $pubpath
Because of the way EasyCounter II keeps track of cumulative accesses, you need to run the counter script once a day. If you run it more often, the cumulative count will be too high. I run my scripts using an "at" command that I have incorporated directly into the script so it will run itself every day without any intervention from me. I have added at commands to the scripts as needed.
If you have not already done so, make a new directory called easycount [mkdir easycount]. The square brackets are for clarity. Do not include them when you type the commands. Change to this directory [cd easycount]. and copy your web page to it. (EasyCounter can count the accesses for more than one html page, but it is easier to explain its operation assuming just one page.) If your page (e.g. mypage.html) is in the public_html directory, the command would look like this:
cp /path/to/public_html/mypage.html mypage.html. Here is how it looks in real life on my system: cp /$HOME/public_html/mypage.html mypage.htmlBe sure to type your real path and real page file name. Now edit mypage.html by deleting the final </body> and </html> tags. (I like Pico, but any editor will do.) These tags should be the last ones on your page.
Here is a list of the files that you will add to the easycount directory. There are a few other files as well, but they will be added automatically when you run EasyCounter II.
#-------------------- getdate.txt (rev. 3/11/97)----------------------- # getdate.txt is a text file that is used to build getdate, an # executable file that is part of EasyCounterII. # getdate creates mylog, a mini access_log of hits to your html # pages. Run getdate anytime tomorrow, and it will log all the # accesses you had today. That way, you get a full 24 hours worth of # accesses (makes it easier to keep the counter). # # define variables # user=sandys # replace sandyste with your name as it appears in access log. # logpath=/carl/httpd/logs/access_log # replace /carl.....with path to your server's access log. # # You should not have to change anything below. # Define the variable, getdate, as the current date, in access_log # format getdate=`date '+%d/%b/%Y'` # This next section applies to the getdate shell program. It # writes lines to it. The getdate program won't actually be # created until makelog is run, so don't worry about it. echo "echo Creating mylog....be patient." >>getdate echo Searching access_log # The following two lines will search only for hits on html pages. # Graphics, e.g. gif, jpg are excluded. Only 200 range and 304 # http response codes are searched for. This will exclude # various non-hits from being counted e.g. 404 not found or # 302 redirection. Including all the html references will give you # an erroneously high hit count. egrep "$yesterday.*$user/.*html...........2.. $yesterday.*$user/.*html...........304" $path >mylog # Don't care about a spuriously high hit count? Comment out the # above two lines and uncomment the following line. #echo "grep $getdate.*$user/.*html $logpath >mylog" >>getda chmod u+x getdate # end of script #-------------------- getdate.txt (rev. 3/11/97)-----------------------Create a file called myhtml.txt. This file will be used to build myhtml, a similarly named executable shell script that will rebuild your html page with the counter on it. What you call ..html.txt should reflect the name of your html page. Do not call it ..html.txt. In this sample, it is called myhtml.txt. Wherever you see references to "myhtml" you should change them to reflect your html page name. For example, if my page is sandy.html, I would have sandyhtml.txt
#---------------------myhtml.txt-----------------------------------
# We copied mypage.html and then edited the file to delete the
# </body></html> tags. We saved the resulting file as
# mypage_short.htm. Save your file with a name that reflects your
# html page. Follow the same format used here.
# We will use mypage_short.htm to re-build mypage.html
#
# User defined variables begin here.
#
start="June 30,1995" # Enter start date for your counter between
# quotes.
page1name=mypage.html # Replace mypage.html with your page name.
page1=mypage.html # Replace mypage.html with your page name.
cp mypage_short.htm $page1 # Replace mypage_short.htm with what you
# called your edited html page.
url="http://www.bcpl.lib.md.us/~sandyste/" # replace with your
# partial site url. Follow same format.
pubpath="/$HOME/public_html" # Enter path to your public_html
# directory in place of/$HOME/public_html if yours is different.
#
# End of user defined variables section.
#
echo "Checking to see if needed files exist. Will build them if needed."
if test -s $page1"_total"
then
echo $page1"_total exists and contains the value:"
cat "$page1"_total
echo "To start counter with a different value, change the number in"
echo $page1"_total"
else
echo creating $page1"_total and initializing counter to 0. To start"
echo "counter with a different value, change the number in"
echo $page1"_total"
echo 0 > $page1"_total"
fi
grep -c $page1name /$HOME/easycount/mylog >"$page1"_newhits
newcount=`cat "$page1"_newhits`
oldcount=`cat "$page1"_total`
cumcount=`expr $newcount + $oldcount`
echo $cumcount >"$page1"_total
# Completing the html page.
echo "<br>" >>$page1
echo "Page accesses on "$yesterday: >>$page1
echo "<b>" >>$page1 >>$page1
echo $newcount >>$page1
echo "</b>" >>$page1 >>$page1
echo "<br>Total accesses since $start: " >>$page1
echo "<b>" >>$page1
echo $cumcount >>$page1
echo "</b>" >>$page1
echo "<br>" >>$page1
echo "<address>" >>$page1
echo "Access counter by <a href=" >>$page1
echo "\"http://www.bcpl.lib.md.us/~sandyste/ezcount.html\">" >>$page1
echo "<b>EasyCounter</b></a>" >>$page1
echo "</address>" >>$page1
# The following four lines are added to the html file to complete it.
echo "<hr>The URL of this page is" >>$page1
echo "$url$page1name" >>$page1
echo "<hr>" >>$page1
echo "</body></html>" >>$page1
# The completed file is copied to the public_html directory.
#cp $page1 "$pubpath"
# Uncomment the above line only after you are sure everything is
# working.
#---------------------myhtml.txt-----------------------------------
Create a file named runscripts. The purpose of runscripts is to
hold the various commands you need to get this stuff operating
properly. Instead of executing several different commands one at a
time, you stick them into runscripts. Then, you
just execute runscripts, which will execute all needed commands.
This will become clear as we move through the explanations. For now,
just put the following lines into runscripts. Then at your unix
system prompt,
# ------------------------runscripts------------------------------ echo "Running runscripts on `date`" getdate myhtml #my2html # If you want to place a counter on other pages, e.g. my2html #my3html # my3html, you would place the commands in runscripts. #my_site.scr # my_site.scr is optional. It will be discussed later. #at -f runscripts 12:30 am tomorrow # If you can run at commands on your server, uncomment the above # line. This is handy for automating your counter. # ------------------end runscripts---------------------------------
The tricky part of this process was figuring out how to count yesterday's accesses. The shell script, makelog, allows you to count yesterday's accesses.
The makelog shell script: Use a text editor to create makelog [pico makelog], and copy this text to it. Make indicated changes to customize for your server and html pages.
#######################################################
# Written by Sandy Steingart April 9 - April 11 1996. #
# Please retain this banner if you use this script. #
#######################################################
#
# After creating makelog, "chmod u+x makelog" so you
# can run it by typing "makelog" (note: don't type the quotes).
#
# Makelog updates a file called getdate. Getdate creates
# mylog, a mini access_log of hits to your html pages
# getdate will search for yesterday's date.
#
# makelog also updates ..html (myhtml) which causes a counter to be
# placed on the html page.
# A file called runscripts is executed via an at -f command at 12:30 am.
# This starts getdate, ..html and my_site.scr. my_site.scr
# builds mysite.html which is a summary of accesses to my website.
# my_site.scr and mysite.html are options and will be discussed
# later.
#
# Define the variable, getdate, as the current date, in access_log format
getdate=`date '+%d/%b/%Y'`
echo yesterday=$getdate >getdate # Gives getdate the date it will
# search for in the system access log.
cat getdate.txt >> getdate # Building getdate by adding text.
# This section updates the files which will put counters on various pages.
# Follow the same format to add a counter to more pages.
# In this example, myhtml is being updated. You need to change this
# to reflect your html pages, e.g. my2html and my2html.txt, my3html
# and my3html.txt.
#
# updating files
echo yesterday=$getdate >myhtml
cat myhtml.txt >>myhtml
#echo yesterday=$getdate >my2html # These two lines are to illustrate
#cat my2html.txt >> my2html # how you could add counters to more
# # than one html page.
# end of updating files.
#
echo "makelog executed for $getdate."
echo "..html and getdate updated."
# Run makelog again tomorrow.
#at -f makelog 1:00 am tomorrow
# Uncomment above line if you can run the at command on your
# server. I hope you can as this is an excellent way to automate
# the process. If you can't run an at command, you will have to
# remember to run this script every day. That will get old quickly.
-rwx------ at the beginning of the line.If not, follow this format to make them executable.
chmod u+x makelogAfter you have created all the needed files, the first script you should run is makelog. Run makelog by typing makelog. Before you run runscripts, open yesterdate and any ..html files you have in runscripts. The first line should say
yesterday=27/Apr/1996The actual date will be the date you do this. Change the date to yesterday's date and save the files. For the above example, I would change it to
yesterday=26/Apr/1996Now run runscripts by typing runscripts. That's it. If you can use the "at -f" command I placed at the end of makelog and runscripts, be sure those lines are uncommented. With the at -f command in place, runscripts and makelog will restart themselves at 12:30 and 1:00 am respectively.
#----------------------my_site.scr-----------------------------------
# When executed, this shell script will summarize yesterday's hits for my
# site.
# /carl/httpd/logs/access_log is the path to the system access log.
# However, I have a mini access_log of hits on my site at mylog.
# I can count hits on any number of pages.
#
echo "<html><head>" >mysite.html
echo "<title>Site Stats for Sandy Steingart</title>" >>mysite.html
echo "</head>" >> mysite.html
echo '<body>' >mysite.html
echo '<h2 align=center>Site Statistics for Sandy Steingart</h2>' >>mysite.html
echo "<hr>" >>mysite.html
echo "Summary of accesses to web site.<br> Page generated `date`." >>mysite.html
echo "Statistics for previous day are reported." >>mysite.html
page1=school_psych.html # Replace school_psych.html with your page name
page2=p_01.html
page3=p_02.html
page4=p_03.html
page5=p_index.html
page6=ezcount.html
page7=cholent.html
page8=mental_health.html
page9=fun_stuff.html
path="mylog" # replace what is between quotes with path
# to your access_log
pubpath="/$HOME/public_html" # Be sure path to your public_html directory
# is here.
pagename1="<a href="http://www.bcpl.lib.md.us/~sandyste/school_psych.html">School Psychology Resources Online</a>"
#pagename1="School Psychology Resources Online"
pagename2="Specific Disorders"
pagename3="Other Resources"
pagename4="What's New"
pagename5="School Psych Index"
pagename6="EZCount"
pagename7="Cholent Corner"
pagename8="Baltimore Area Mental Health"
pagename9="Fun Stuff"
echo "<ul>" >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename1:" `grep -c $page1 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename2:" `grep -c $page2 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename3:" `grep -c $page3 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename4:" `grep -c $page4 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename5:" `grep -c $page5 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename6:" `grep -c $page6 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename7:" `grep -c $page7 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename8:" `grep -c $page8 $path` >>mysite.html
echo "<li>" >>mysite.html
echo "$pagename9:" `grep -c $page9 $path` >>mysite.html
echo "</ul>" >>mysite.html
echo "<hr>" >>mysite.html
echo "<h4>Visitors to School Psychology Resources Online</h4>" >>mysite.html
echo "<pre>" >>mysite.html
echo "`grep school_psych.html mylog | cut -f1, -d' ' | uniq `" >>mysite.html
echo "</pre>" >>mysite.html
echo "<h4> Visitors to Fun Stuff</h4>" >>mysite.html
echo "<pre>" >>mysite.html
echo "`grep fun_stuff.html mylog | cut -f1, -d' ' | uniq `" >>mysite.html
echo "</pre>" >>mysite.html
echo "<hr>" >>mysite.html
echo "</body></html>" >>mysite.html
cp mysite.html $pubpath
echo "my_site.scr executed. Site statistics summary page generated."
#------------------------------my_site.scr--------------------------
#------------------ custom getdate.txt (rev. 3/11/98) ---------------------
# getdate creates mylog, a mini access_log for my html pages.
# This version is cutomized for bcpl.net
# It searches access_log and access_log0
# Check for mylog and remove it if it exists.
if [ -r mylog
then
rm mylog
echo "Old mylog deleted"
else
echo "mylog does not exist"
fi
path=/carl/httpd/logs/access_log # path to current access log.
path0=/carl/httpd/logs/access_log.0 # path to old access log.
user=sandyste # username as it appears in access log.
echo Creating mylog....be patient
echo Searching access_log and access_log.0
egrep "$yesterday.*$user/.*html...........2..
$yesterday.*$user/.*html...........304" $path >mylog
egrep "$yesterday.*$user/.*html...........2..
$yesterday.*$user/.*html...........304" $path0 >>mylog
#---------------end custom getdate.txt (rev. 3/11/98) ---------------------
#--------------------- EasyCounter Ia --------------------------
# Assume you want to add a counter to example.html.
# If you haven't already done so, make a directory called easycount.
# Copy example.html to easycount directory. Edit the file to delete the
# final </body> and </html> tags. Save the resulting file
# as example_short.htm.
# We will use example_short.htm to re-build example.html. Don't
# forget to change the "example" names to something that reflects
# your html page name. This is not a speedy counter, so be patient when
# you run it. You might want to run this as a cron file. See a unix book
# for instruction on how to do that. Once you are sure this works,
# uncomment the next to the last line in order to copy your web page to
# your public_html directory.
#
# User defined variables. Enter needed values as indicated.
#
start="June 8, 1995" # Enter starting date for your counter.
url="http://www.bcpl.lib.md.us/~sandyste/example.html" # Enter your
# page url
user=sandyste # Replace sandyste with your userid as it appears in
# access_log
pubpath="/$HOME/public_html" # Enter path to your public_html
# directory in place of/$HOME/public_html if yours is different.
page1name=example.html # Replace example.html with your page name
page1=example.html # Replace example.html with your page name
cp example_short.htm $page1 # Replace "example" with your page name.
#
# This ends user defined variables. You should not have to modify rest
# of script.
daymo=`date '+%d'` # the current day of the month, e.g. 01, 10, etc.
path=/carl/httpd/logs/access_log # path to current access log.
path0=/carl/httpd/logs/access_log.0 # path to old access log.
echo Checking logs....be patient.
if [ $daymo -eq 01 ]
then
if test -f ranit # This is an indicator file. Tells us if we ran this.
then
echo "already ran this at least once today (first of month) so"
echo "no need to search access_log.0 again."
grep -c $user/$page1name $path >newcount
else
echo "Running script for first time today (first of month). Will"
echo "search access_log.0 and access_log and update cum. count"
cat oldcount >oldoldcount
grep -c $user/$page1name $path0 >oldcount
ooc=`cat oldoldcount`
ocnt=`cat oldcount`
newoc=`expr $ooc + $ocnt`
rm oldcount
echo $newoc >oldcount
echo "Searching access_log"
grep -c $user/$page1name $path >newcount
echo "ran it" >ranit # Creating the indicator file.
fi
else # Section below runs every day except first of month.
echo "Today is not the first of month. Searching access_log"
grep -c $user/$page1name $path >newcount
rm ranit
fi
echo "Checking to see if oldcount file exists. Will build it if needed."
if test -s oldcount
then
echo "oldcount exists and contains the value:"
cat oldcount
echo "To start counter with a different value, change the number in"
echo "oldcount."
else
echo "building oldcount and initializing it to 0. To start with a"
echo "different value, change the number in oldcount"
echo 0 > oldcount
fi
# updating the cumulative count
oc=`cat oldcount`
nc=`cat newcount`
cumcount=`expr $oc + $nc`
# Adding the access count to the html page.
echo "<br>" >>$page1
echo "Accesses since "$start": " >> $page1
echo "<b>" >>$page1
echo "$cumcount" >>$page1
echo "</b>" >>$page1
echo "<address>" >>$page1
echo "Access counter by <a href=" >>$page1
echo "\"http://www.bcpl.lib.md.us/~sandyste/ezcount.html\">" >>$page1
echo "<b>EasyCounter</b></a>" >>$page1
echo "</address>" >>$page1
# The following four lines are added to the html file to complete it.
# If you don't want the page url included, comment out the next 3 lines.
echo "<hr>The URL of this page is" >>$page1
echo "$url" >>$page1
echo "<hr>" >>$page1
echo "</body></html>" >>$page1
# The completed file is copied to example.html in the public_html
# directory. Uncomment the following line when the script works to
# your satisfaction
#cp $page1 "$pubpath"
echo "Finished. Check web page for counter."
#--------------------- EasyCounter Ia --------------------------
Major changes Mar 11, 1998, Apr. 24, 1996. Comments to Sandra Steingart
sandyste@umd5.umd.edu