Tutorials

Active users counter

This involves the use of a global.asa file.

This tutorial will not go into the in's and out's of what a global.asa file is because that would take too long (check out this article on 4guysfromrolla for further information). Instead, what I will do is simply show you the code you need in the global.asa file and on your web page to create and use and active users count.

This global.asa file should be put in the root of your website.

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
  Session.Timeout = 20
  Application.Lock
  Application("Active") = 0
  Application.UnLock
End Sub
Sub Session_OnStart
  Application.Lock
  Application("Active") = Application("Active") + 1
  Application.UnLock
End Sub
Sub Session_OnEnd
  Application.Lock
  Application("Active") = Application("Active") - 1
  Application.UnLock
End Sub
</SCRIPT>

Notes

Here we have set up a variable called Active.

We initialise it at 0 when the application is first started (which will occur the very first time the site is used, including when the server or IIS has been restarted) and from then on we add one to it whenever a new session is created on the site, or we deduct one from it whenever a session ends.

To display this data on your web page, all you need to do is call the variable.

<%= Application("Active")%>

It really is as simple as that!