Counting how many users you have online

Session Object

The session object, a built-in ASP object, refers to a new client accessing your webpage. For instance, when a user enters your site, the server adds a special cookie to the visitors browser, which is called the ASPSessionID cookie. This ASPSessionID cookie can be very useful when you need to identify a particular user.

Randomize

Session(“user”) = rnd()

The code above will give the session variable user the value of a random number. This will identify this user throughout their journey until they leave our site or close their browser.

The Application Object

The application object is another built-in ASP Object that works almost like the session object except it refers to all users. For instance, if you create a application variable named id and give it the value of 21, all users viewing your site would have a application variable named id with the value of 21.

application(“id”) = 21

The Global.asa File

So what’s the Global.asa file? The Global.asa file executes event handlers when a user first comes to our site. What are event handlers? Event handlers describe what needs to happen at a certain event. There are two events the session and application objects support. The On_Start and On_End event. When a user first enters our site, the On_Start handler is triggered and any code inside the On_Start handler is executed. And as you would guess, when a user leaves our site or closes their browser, the On_End handler is triggered and any code inside the On_End handler is executed.

<Script Language=VBScript RUNAT=Server>

Sub Session_OnStart()

Randomize

Session(“user”) = Rnd()

End Sub

The first line of code is a script tag which specifies that VBScript is the language we are using and it will run on the server. We then create a subroutine which is triggered when a user enters our website and takes the session variable user and gives it the value of a random number.

Creating the Code

Now, in order to create a script that counts how many users a currently viewing our site, we must use both the application and session objects with both On_Start and On_End event handlers.

global.asa

<SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”>

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>

The code above will count how many users are currently viewing our site. If you are a little confused, don’t worry. We will dissect the code line by line.

The first line of code as I said above specifies that VBScript is the language we are using and it will run on the server.

The third line of code creates a subroutine which is called when a user enters our site.

Now the next line of code is new. We haven’t discussed it in this tutorial. The application object has the lock() and unlock() methods. When you lock the application object, you ensure that no one else can edit it while it’s being edited. And the unlock() method unlocks the application object so it can be edited once again.

After we lock the application object, we edit the application variable activeusers and add it by 1 since a new user has entered our site. Now to some people, this is confusing at first. Why do we use a application variable instead of a session variable? The reason for this is because, the session object is used to only refer to one user while the application object refers to all users. If we used a session object instead of a application object, all of our users would have a session variable named activeusers equal to 1 which would be no good to us. In order for use to track how many users are viewing our site, we must first track each user that enters our site by using the Session On_Start event and then add the application variable activeusers by 1, which refers to all users who have entered our site.

Now we can unlock the application object so the next user can use it.

The next chunk of code is just the same as the code above, except it is triggered when a user either leaves the site or closes their browser. When this happens, the application variable activeusers is subtracted by 1 since a user has left our site.

The last line of code is our closing script tag.

So how do you display the number of active users currently viewing your site? You place the following line of code in one of your asp pages:

index.asp

Response.Write Application(“activeusers”)
Author: Andrew Schools