I wrote this sample to answer the question that I'm sure has been
on everyone's mind: "How long can an ASP reference site go before
putting up a sample hit counter script?" Well whatever the old
record was I think we probably shattered it! I've finally broken
down and decided to write one. It's pretty simplistic and doesn't
really show off the power of ASP, but what do you want... after all
it is just a counter!
It stores it's value in a textfile and counts each request. I
didn't bother with a session count. If you're interested in that
see my article "Counting
Active Users!" and just remove the line that decrements the
count!
What I did do is take into account some of the things that you might
want in a hit counter. First of all you can use this script two
different ways. You can cut and paste it into the page you
want the count to appear on or you can use it as an include
file and just place one line of text in the document where you want
the count to appear. The text should look something like this:
<!-- #INCLUDE FILE="counter.asp" -->
One thing to note about using this syntax is that the count file it
creates will be wherever the file doing the including is located and
this is not necessarily where the script is located! This may be
confusing at first, but it allows you to include the same file and
run multiple independent counters on different pages with only one
set of the code!
Another thing to note is that it'll create it's own file to keep
track of the count if it doesn't find one. This makes it pretty
darn easy to install. The main problem you might run into is if
NTFS permissions don't allow the anonymous internet user to write
to the counter.txt file. Once you get any NTFS issues straightened
out all should be right with the world!
The last thing that bears mentioning is that, as it stands, the
script will let you choose either plain text or a set of images.
You can toggle this by setting the images querystring parameter
to true or false. If you always use one or the other it probably
makes sense to hard code the value in, but for illustration and
flexability I let you change it on the fly. And for those of you
who actually care (and are still reading this) the default is to
use text.