2:27 pm Saturday, July 05, 2008


  Navigation
  Help?
Contact Aaron for all inquiries.
 

  News

ASP Inline Calendar 3.9 Beta 1
ASP Games
ASP Inline Corporate Calendar 3.8 BETA 1 Demo
Calendar Updates
 

A Better World By Design
 
  Session Hit Counter Tutorial
AaronOutpost : ASP / Tutorials / Counters / Active User Hit Counter Using Global.asa

Active User Hit Counter Using Global.asa Tutorial
By: Aaron B. Copyright AaronOutpost.com

 

First off, we get to do what I call the fun part.... Make the Graphics!!!


Counters Tutorial

  1. Create a Text Hit Counter

  2. Create a Graphic Hit Counter

  3. Create a Text or Graphic Session Hit Counter

  4. Create an Active User Hit Counter using global.asa

  The IDEA
The whole point of having an active user counter is to count how many users have on your entire site. So, why not have a file that runs no matter where they are on the site and you don't have to include this file in every single file on your site!!! Sound nice!? You better believe it. The idea behind the global.asa file is that it runs no matter what file is opened on the site. Allowing you to enter codes that you want to run no matter where they are on your site. Let's Begin!


Begin writing the code for global.asa :

First we need to have the server start code when first user uses the site. The Application_OnStart variable will remain active until the server is stopped or is rebooted. We also need to create a variable to count the active users and we start it off at zero.

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

Sub Application_OnStart
Application("ActiveUsers") = 0
End Sub

 

 

This is the code that actually tells it to run the sub function when any page is accessed.

Sub Session_OnStart

 

 

Next we need to set a session timeout. This is how long a session connection to the user will stay active. Default is 20 minutes. You don't want to set this number to low, if the user is browsing photos or reading, they may not click another link before the timeout occurs and on the other case, the user could have left the site but is still responding as if they are there.

Session.Timeout = 30

 

 

Lock the application so a user can only increment the count by one.

Application.Lock

 

 

Now we increment the count by one, unlock the application and then end the sub routine.

Application("ActiveUsers") = Application("ActiveUsers") + 1
Application.UnLock

End Sub

 

This is for when the user leave the site or session times out. Server usually can't detect when user leaves if they are disconnected so it will wait for the time out to occur. Either way.

Sub Session_OnEnd

 

 

We lock the application again so the Active user count can only decrease by one for that user. Then substract one for that user and unlock the application again. We end that sub routine and end the script and this "bad boy" is done!

Application.Lock
Application("ActiveUsers") = Application("ActiveUsers") - 1
Application.UnLock
End Sub

</SCRIPT>

 

Begin writing the code for default.asp :

We must also write a code if we used to display these values. This is also going to lead back to my other tutorials on how to create a loop so you can display graphics instead of text. The tutorial is complete in teaching the method but you may also want to view these other counters.

You can place this code into any type of HTML or ASP page, Just be sure you include it in it's entirety.

First off need to dimension a loop variable.

<%
'Dimension variables
Dim LoopCounter

 

Incase a user happen to access the global.asa file simultaneously or some other odd error, this code keeps the error from occurring by telling it to go on to next.

On Error Resume Next

 

First line is just response writing title Active User's

The next line of code is saying for the variable LoopCounter go from 1 to the length of number in the Active Users variable from the global.asa file. The Len command counts how many number there are. So if the number is 995 there are 3 number so it would loop from 1 to 3.

%>
 <font size=2>Active User's Graphical Version</font><br>
<%
For LoopCounter = 1 to Len(Application("ActiveUsers"))

 

Now we use the mid function to display the counter images. So we response.write HTML code with the directory to pictures, then we use the Mid function and we write the current number in count with .gif so it will display the correct gif and then it will loop to the next. You can change gif to jpg or whatever you image format is.

Response.Write "<img src=""counter_images/" & Mid(Application("ActiveUsers"), LoopCounter, 1) & ".gif"">"
Next
%>

 

You can also display the count in a text version using the following. Simple.

<font size=2>Active User's Text Version</font><br>

<% Response.Write Application("ActiveUsers") %>

 

 

 

Finally this finished project should look something like this! Make sure it is saved in .asa & .asp format as mentioned before and must be run on an ASP2 enabled server.
 Global.asa File
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

'----------------------------------------------------------------
'-- SUB AT APPLICATION START
'----------------------------------------------------------------
Sub Application_OnStart
Application("ActiveUsers") = 0
End Sub

'----------------------------------------------------------------
'-- SUB AT SESSION START
'----------------------------------------------------------------
Sub Session_OnStart
Session.Timeout = 30
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers") + 1
Application.UnLock
End Sub

'----------------------------------------------------------------
'-- SUB AT SESSION END
'----------------------------------------------------------------
Sub Session_OnEnd
Application.Lock
Application("ActiveUsers") = Application("ActiveUsers") - 1
Application.UnLock
End Sub

</SCRIPT>

 Default.asp File
<%
Dim LoopCounter
On Error Resume Next
%>

<font size=2>Active User's Graphical Version</font><br>

<%
For LoopCounter = 1 to Len(Application("ActiveUsers"))
Response.Write "<img src=""counter_images/" & Mid(Application("ActiveUsers"), LoopCounter, 1) & ".gif"">"
Next
%>

<font size=2>Active User's Text Version</font><br>
<% Response.Write Application("ActiveUsers") %>

View Active User Hit Counter Example

 


AaronOutpost : ASP / Tutorials / Counters / Active User Hit Counter Using Global.asa



Aaron Outpost West Virginia, USA A Mark of _-Squire-_ Creations Inc.  CopyrightŠ 2005 All Rights Reserved