
This tutorial will show you how you can create a very primitive and simple User counter that represents how many users are currently online and viewing web sites. The tutorial will use information learned in the other tutorials below:
If you are unfamiliar with the Global.asa file, or the ASP Application
object then you should take the time to review the above tutorials
before proceeding with this tutorial.
The first thing we need to do is to discuss how this counter will function and what it will do. In this case, the counters purpose is straight forward - we want to display on our web site how many people are currently viewing it. Now that we have that out of the way, we can start building it.
Basically what we need is a global variable that keeps track of how many people are currently online. We can use the Application Object for this and use an Application variable so taht everyone has access to the same variable simultaneously.
Next we need a way to determine when a new user visits the site, and to increment the new variable. Using the Globa.asa file along with the Session_OnStart event, we can do this quite easily.
The last part we need is something that will decrease the value of the variable when people leave the site. Again, the Global.asa file can be used to set this up using the Session_OnEnd Event.
The first thing we need to do is to create our global.asa file, (that is, if your current web site does not already have one). We will need three of the four possible events that can be used in the Global.asa file:
The code for our new global.asa file appears below:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
![]()
Application("ActiveUsers")
= 0
End Sub
Sub Session_OnStart
Application.Lock
Application("ActiveUsers")
= Application("ActiveUsers") + 1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("ActiveUsers")
= Application("ActiveUsers") - 1
Application.UnLock
End Sub
< /SCRIPT>
Now that we have our counter code setup in the global.asa file, we can display it's value on any asp page in the web site by calling it from the Applications Contents Collection.
<%= application.contents("ActiveUsers") %>
If you are feeling adventurous, you could easily turn this into a graphical ouput. All you wuold have to do is create 10 images of the individual numbers 0-9 and store them sowehere on the web server. Then all you do is convert the number returned by the above line in to a string. You can then split the string in to individual numbers and display one of the 10 images according to the value of each character. Simple!
Tutorial by Justin Kercher
2005.
This document may not be copied for use on another site.
Copyright© 2005.