Hi Hugh,
You're heading in the right direction, but first you need to make a few
considerations.
You say that you're making your default home page file name "default.asp" so
that you can get a count of the hits to this page. However, you're
redirecting to "default_home.htm" for no apparent reason. If you're counting
on this page, why not put your content on this page as well? The redirect is
a waste of resources and user's time. Also, your method will not yield any
accurate statistics, since it requires the user to go to the default home
page in order to be counted. What happens when, for example, a user creates
a bookmark to one of your pages and goes directly there wihtout going to
your home page? Nothing, because your counter is on that one page, and that
page only.
First, therefore, you need to decide what exactly it is you want to count.
Do you want to count the specific number of times that a user requests a
certain page? That's what your current code will do. It will keep track of
each and every hit to a single page. Most often, what is useful to the
Webmaster is the number of unique User Sessions on the site (visits to
various pages on the site within a single Browser Session) happen in a day
or over a period of time. I will assume that User Sessions is what you want
to count, and that you don't want to count only a single page, but the
entire Session.
To do this, you need to use a Global Event Handler. In ASP, there are 3 that
we will want to look at: Application_OnStart, Session_OnStart, and
Session_OnEnd. In your code, for example, you are trying to increment an
Application variable. Where does it get created/initialized? This would be
in the Application_OnStart Sub:
Application("counter") = 0
Now, in order to count the User Session, we use the Session_OnStart sub,
which fires exactly ONCE per User Session, and is not page-specific.
Regardless of which page a user requests, this Sub in the global.asa file is
fired:
Application.Lock
Application("counter") = Application("counter") + 1
Application.Unlock
Now, keep in mind that Application variables are stored in memory, so you
want to think about persisting this data somewhere, depending on what
exactly you want to do with it. This can be done by writing it to a text
file, database, or any other persistent storage medium.
If you want to keep track of how many users are currently online with your
site, you can user another variable and the Session_OnEnd Sub. In the
Application_OnStart Sub, you create and initialize a variable like we did
with the "counter" variable:
Application("UsersOnline") = 0
In the Session_OnStart:
Application.Lock
Application("UsersOnline") = Application("UsersOnline") + 1
Application.Unlock
In the Session_OnEnd Sub:
Application.Lock
Application("UsersOnline") = Application("UsersOnline") - 1
Application.Unlock
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.