Home

ASP ADVANCED

Good Webmasters have a single purpose in life: They want more users to visit their Web sites.  Part of this obsession involves accurately tracking the number of users currently online and determining what they're doing.

The project described in this section allows you to track the visitors to your Web site in real time.  You can determine the number of visitors to your Web site at any moment.  You can also learn the last page requested by each of your visitors.

This project illustrates how to assign an object to an application variable.  The Dictionary object will be used to store information about visitors.  Whenever a user requests a page, the information in the Dictionary object will be updated. The following files need to be created or modified for this project:

  • The Global.asa file.  Both the Application_OnStart and session_OnEnd scripts need to be modified for this project.
  • The Grabstats file.  This file updates the Dictionary object.  You'll need to include this file in every page you want to track.
  • The WhosOn page.  The WhosOn page displays the users currently at your Web site.

Modifying the Global.asa File

To create this project, you need to modify two scripts in the Global.asa file.  First, you will need to create the Dictionary object, which is used to store the information about your visitors.  Because this object needs to be created only once, this is accomplished in the Application_OnStart script:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnStart
Set Application("Stats") = Server.Create0biect("Scripting.Dictionary")
END SUB
</SCRIPT>

A single line has been added to the preceding script.  The statement assigns an instance of the Dictionary object to the application variable named stats.  Once created, this variable can be used throughout your application.

The Session_OnEnd script in the Global.asa file also must be modified.  The purpose of the following script is to remove a user from the Dictionary object when the user's session ends:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnEnd
IF Application("Stats").Exists(Session.SessionID) THEN
Application.Lock
Application("Stats").Remove(Session.SessionID)
Application.Unlock
End IF
END SUB
</SCRIPT>

Users will be tracked by their session IDs.  The keys in the dictionary named stats correspond to these session IDs.  The preceding script checks whether an entry with the session ID of the current user exists in the dictionary.  If it does, the entry is removed.

Creating the GrabStats File

To determine the current page of your visitors, you need to include a file in every page you want to track.  The file consists of a single-line script, as shown in Listing below:--

<%
Application("Stats").item(Session.SessionID)= Request.ServerVariables("SCRIPT_NAME")
%>

This script adds the path of the current page to the Dictionary object.  The path of the current page is determined by retrieving the SCRIPT_NAME server variable.  Next, the value of this variable is assigned to the dictionary key that corresponds to the current user. (If the key doesn't exist, it's created automatically.)

Save the preceding file with the name GrabStats.asp.  You should include this file in every Active Server Page that you want to track.  Simply add the following line to the top of an Active Server Page:

<!-- #INCLUDE VIRTUAL="GrabStats.asp" -->

Creating the WhosOn Page

The WhosOn page is used to display the current visitors.  The session ID of each visitor is displayed next to the last page that the visitor requested.  Listing below shows the WhosOn page.

<I-- #INCLUDE VIRTUAL="GrabStats.asp" -->
<%
MyServer=Request.serverVariables("SERVER_NAME")
MyPath=Request.ServerVariables("SCRIPT_NAME")
MySelf="HTTP://"&MyServer&MyPath
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="20;<%=MySelf%>">
<TITLE>WHOSON</TITLE>
</HEAD>
<BODY>
<%
Application.Lock
Set TempStats=Application("Stats")
Application.UnLock
%>
<CENTER>
<B>TOTAL USERS:</B> <%=TempStats.Count%>
<TABLE BORDER=1 CELLPADDING=10>
<TR><TH>User ID</TH><TH>Current Page</TH></TR>
<%
TempItems=TempStats.items
TempKeys=TempStats.keys
For i=0 to UBOUND(TempKeys)
%>
<TR><TD><%=TempKeys(i)%></TD><TD><%=TempItems(i)%></TD></TR>
<%
NEXT
%>
</CENTER>
</TABLE>
</BODY>
</HTML>

The first line in this file includes the GrabStats.asp file that you created in the previous section.  This allows you to know, when you are viewing this page, that you actually are viewing this page.  The path of the WbosOn page will appear next to your session ID.

The first script is used to retrieve the path of the current page.  The WhosOn page is automatically refreshed, using client-pull, every 20 seconds.  The path of the current page is needed so the page can be refreshed.

The second script transfers the dictionary stored in the application variable to a temporary Dictionary object named TempStats.TempStats is automatically destroyed when the page ends.

The number of current visitors is retrieved by returning a count of the number of items in the dictionary.  The final script displays all the entries in the dictionary by looping through its keys and items.

Extending the WhosOn Page

As it stands, the WhosOn page provides valuable information about the users of your Web site.  By viewing the WhosOn page, you can gather a rough estimate of the number of users currently at your Web site.  You can also see the last page each user requested.

There are a number of ways in which this project could be extended.  For example, if your web site requires users to register, you could display user names rather than session IDs.  To do this, simply use visitor names as the keys in the dictionary rather than session IDs.

Furthermore, it would be useful to know how long each visitor has been online.  You could modify, the project in a number of ways to track this information.  For example, you could simply create a second application variable that contains a second dictionary.  In that case, you could track the length of each user's visit by storing this information in the second dictionary.