Show current user count on your web
site.
Many people ask me how I do this so here is the code I use. There is not a
lot to this and this isn't going to give you a totally accurate count, but
it works well enough. I find that the busier your site is the more accurate
you will find it to be.You
see, there is a file called the "global.asa". The Global.asa file is event driven. It can
contain four event procedures: Application_OnStart,
Application_OnEnd,
Session_OnStart, and
Session_OnEnd.
These event procedure stubs can contain
script you want to be executed when the application starts or ends, or when
the session starts or ends.
Now, to get a user count the code below goes in your "global.asa" file which needs to go in the root of
your web site. You web site must be properly set up as an application in IIS
for this file to fire and do it's thing.
<SCRIPT
LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Application("WhoOn") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("WhoOn") = Application("WhoOn") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("WhoOn") = Application("WhoOn") - 1
Application.Unlock
End Sub
</SCRIPT> |
Then to display the count on any ".asp" page in your web site just do
something like this.
<%= Application("WhoOn") %> USERS CURRENTLY ONLINE
Now, the Session_OnEnd part basically means that if a user has no activity
for a period of time that (1) will be subtracted from the current user count.
That time period being whatever you have the Session.Timeout set to. This means
that if your Session.Timeout is set to 20 minutes that it may take up to 20
minutes for the count to update after a user leaves.
The other thing to realize is that a user can inflate the count by closing their
browser and coming back to your site over and over. This problem takes care of
it itself over time as all those counts will eventually expire and go away on
their own but it is something to be aware of. Sometimes you'll notice this
happen and it will be some jerk messing around to see if they can make your
count go up. Other times it will be search engine bots indexing your site that
cause the count to spike.
Also remember that if the web is not set up to run as an application the "global.asa"
will not run. You'll need to make sure the web is an application. Most Virtual
Domains are by default, but sub webs usually are not.
For the sub webs to run the 'global.asa" they need to be an application as the
root usually is. In NT this is accomplished via the Internet Service Manager
under the properties of the sub web you want to make an application
|