How Many People Are on your Site Right Now?
By Joćo Vieira
I think this one is really easy, and even if you never used global.asa don't worry, just
put the one I have at this page for download at the root of your page (e.g.
If you are unfamiliar with global.asa, be sure to also check out
this article on Global.asa.
Now all you need is to keep track of a counter as each new user enters
your page. You'll also need to decrement your counter when someone leaves,
and have code to display the current count.
Explaining Application Variables
Application variables work like session variables except that the value of Application variables are the same for all the users, and the values of session variables are different for each user. An instance of every session variable is created for every user. Only one instance of all Application variables is created when the server starts up. Hopefully with the example you will understand better.
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart ' Sub Application_OnStart is the procedure that fires ' everytime your server starts up. ' Here you want to define the time that the user ' will be valid as on-line. (If no activity occurs ' for x minutes, then log the user off (erase his ' session variable instances)) Session.Timeout = 3 ' The .Lock method locks the Application variable so that ' you can work on it. If you don't lock it there won't be ' any changes on the variable "WhosOn" Application.Lock ' Start the variable at 0, Application("WhosOn") = 0 ' You now need to unlock the application Application.UnLock End Sub
Sub Session_OnStart ' Sub Session_OnStart is the procedure that works ' everytime a new user enters in your page ' Here is the place where you increment the number ' of users on your page (don't forget to Lock/Unlock ' your Application variable!) :) Application.Lock Application("WhosOn") = Application("WhosOn") + 1 Application.UnLock End Sub
Sub Session_OnEnd ' Sub Session_OnEnd is the procedure that works ' everytime a user quits the page, this is defined n ' minutes after he quits. ' This n minutes are defined by the Session:Timeout in '
Sub Application_OnStart' So... when a user quits there will be one less user, so we ' decrement one on the "WhosOn" variable (don't ' forget to Lock/Unlock your application variable! :) Application.Lock Application("WhosOn") = Application("WhosOn") - 1 Application.UnLock End Sub
We'll,... after lots of research I made this code that show the users that are seeing your page on real-time by getting the info from the application variable...
<% response.write "Are Now " & Application("WhosOn") & " users at this page." %>