Page

8.5.0- Sessions and global.asa

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 08. Applications, Sessions and Cookies.

Not tagged.
<< 8.4.3- Session Object MethodsChapter88.5.1- Cookies - The End of the Line? >>

Sessions and global.asa

Earlier in this chapter, we saw how the global.asa file can be used to process the startup and the shutdown of web applications. The file can also be used to handle events fired by the startup and shutdown of user sessions within a specific web application. Just as there are two event handlers for the Application object, there are two for the Session object as well.

Session_OnStart

Session_OnStart is called every time a new user begins a session with the web site. If you want to initialize a session-scoped variable in Session_OnStart, you'd use:

 

Sub Session_OnStart

Session("YourVariable") = "SomeValue"

End Sub

 

For example, let's say you want to track the number of visitors currently accessing your site. (This is different from a traditional counter which tracks total numbers of visitors.) To do this, you would have an application-level variable that tracked the current number of users that were currently accessing the site. This is the same number of sessions that are currently active. Every time that a new session was started, we would want to add one to that number, and that can be done in the Session_OnStart routine.

Session_OnEnd

Session_OnEnd is called whenever a session terminates. This can happen if the session times out, or if it is explicitly abandoned. This isn't the same as just closing a browser down, in which case the session has to time out, before it ends. In Session_OnEnd, you might want to transfer temporary session variables to databases, or set application-level variables to another value: for example, if you are tracking the number of users currently visiting a site in an application-level variable, then you would subtract one from this number every time Session_OnEnd was run.

 

Sub Session_OnEnd

Application.lock

Application("Active") = Application("Active") - 1

Application.unlock

End Sub

Declaring Session Objects in global.asa

You can also declare session-scoped objects in global.asa:

 

<OBJECT RUNAT=Server SCOPE=SESSION ID=Session PROGID="progID">

</OBJECT>

 

<OBJECT RUNAT=Server SCOPE=SESSION ID=Session CLASSID="ClassID">

</OBJECT>

 

You can use either ProgID or ClassID, but not both.

 

As with application objects, session-level objects declared in global.asa are not created until the server processes a script that calls the object. Again, this saves resources by only creating objects that are actually used.

Try It Out – Visitor Tracking Using global.asa

In this example, we will take what we've learned and construct a global.asa file that will allow us to track the number of current visitors to the application, as well as the total that have visited since the application was started.

 

Unfortunately, since some of you might not have the facility to use multiple machines to track sessions, we're going to have to 'cheat' a little bit, to demonstrate the code working. We'll explain how the 'cheat' affects what we see, as we go along.

 

1.    Create a new folder called Session, within the c:\Inetpub\wwwroot folder.

2.    Start up MMC and right click on the Session folder and select the Properties option to bring up the Properties dialog.

Chapter8_image013

 

 

3.    Click on the Create button to create a new application, and click on OK to close the dialog. (The folder must be turned into an application for this example to work.)

4.    Create a new file, and key in the following:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart

Application("visits") = 0

Application("Active") = 0

End Sub

 

Sub Application_OnEnd

 

End Sub

 

Sub Session_OnStart

Session.Timeout = 1

Session("Start") = Now

Application.lock

Application("visits") = Application("visits") + 1

intTotal_visitors = Application("visits")

Application.unlock

Session("VisitorID") = intTotal_visitors

 

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>

 

5.    Save this file as global.asa. For this example, you need to store your global.asa file in a different directory. Store it in your Session folder, which should be found under C:\InetPub\wwwroot\Session.

If you are downloading the code for this chapter from the Wrox website, you will find that this file is called global_session.asa. You will need to place it in the correct directory and rename it to global.asa in order for this example to work.

6.    Create a new file, and key the following:

 

<HTML>

<HEAD>

<TITLE>Retrieving Variables Set in Global.asa</TITLE>

</HEAD>


<BODY>

<P>

There have been <B><%= Session("VisitorID")%></B> total visits to this site.

<BR>You are one of <B> <%= Application("Active")%></B> active visitors.

<BR>Your session started at <%= Session("Start") %>

 

</BODY>

</HTML>

 

7.    Save this file as VisitorCount.asp. You will need to save it in the Session folder, as it needs to be in the same folder as the global.asa.

8.    View the file in your browser.

Chapter8_image014

 

9.    View the file in another browser or in another instance of your browser (if you simply open a new window, you will find that this won't work).

 

Chapter8_image015

 

 

10.  Close down the second browser immediately and press the Refresh on the first (time is of the essence as we'll explain shortly).

Chapter8_image016

 

Note that the number of active visitors is two, even though we've just closed a session, we'll explain shortly. Also note that the number of total visits is still only one on this browser, even though two browsers have visited the site. This is because the number of total visits is actually set when the session is started, and not updated throughout. So when this browser viewed the site, it was the only browser to have viewed this site. Even though another browser has viewed this site, the total remains at one. So while strictly speaking our total number of visits is accurate for the beginning of the session, it doesn't adjust dynamically throughout the session.

 

11.  Wait one whole minute and press the Refresh on the browser again

Chapter8_image017

 

As explained earlier, we can't really mimic the action of several users browsing our site (unless you have a few spare PCs handy, or a lot of friends), so what we've done is set the value of the Session.Timeout to one minute in global.asa. When you close a browser your session doesn't automatically end, but the session will timeout after a minute, and be ended for you. Only then is the number of active users is reduced. So our count is accurate, but it takes a little while to update.

How It Works

In this application, we are using application-level variables to track the number of users that are currently looking at a site, as well as the total number that have accessed it.

 

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart

Application("visits") = 0

Application("Active") = 0

End Sub

 

In the first event handler of the global.asa file, we will be working with the Application_OnStart method that is called whenever the application is started.

 

There are two variables that we will need to maintain throughout the life of the application. The visits variable will hold the total number of visits since the application was started. One thing to note is the Visits variable is initialized as zero. That means every time the application is unloaded, and restarted, this variable will be set to zero. If you don't want this to happen, each time you restart the application after unloading it, we suggest you remove the zero. The Active variable will hold the number of current user sessions in the application.

 

Sub Application_OnEnd

 

End Sub

 

We will not be doing any processing when the application is ended, so we are just adding this event handler as a placeholder.

 

Sub Session_OnStart

Session.Timeout = 1

Session("Start") = Now

 

When a session is started, we will perform some processing in the Session_OnStart event handler. To make our example work faster, we set the Timeout value of the Session object to 1 minute. In a real application, you would very rarely have your Timeout value set so low, but then again you would have people visiting your application using different machines, rather than having to rely on the one machine we are using.

 

We will also store the starting date and time for this session into a session-level variable.

 

Application.Lock

Application("visits") = Application("visits") + 1

intTotal_visitors = Application("visits")

Application.Unlock

Session("VisitorID") = intTotal_visitors

Next, we will need to find out the total number of visitors to the site since the application was started. This information is stored in an application-level variable. In order to change an application-level variable, we need to first Lock the Application object. We can then increment the value stored in the visits variable by one. This new value will represent the total number of visitors, including the one currently starting. The incremented value is stored back into the same application-level variable as it came from. Once we have made all the changes that we need to at this point, we will call Unlock to free up the Application object.

 

Application.Lock

Application("Active") = Application("Active") + 1

Application.Unlock

 

Finally, we will update the current number of active users by one. This information is stored in another application-level variable.

 

Sub Session_OnEnd

Application.Lock

Application("Active") = Application("Active") - 1

Application.Unlock

End Sub

 

When the user leaves the site and their session times out, this event handler will be called. The important thing that needs to happen in this file is for the number of active users to be decremented by one. Since this information is stored at the application-level, you will need to Lock the Application object when making the change, and then Unlock the object as quickly as possible.

 

There have been <B><%= Session("VisitorID")%></B> total visits to this site.

<BR>You are one of <b> <%= Application("Active")%></B> active visitors.

<BR>Your session started at <%= Session("Start") %>

 

In our test page, we will be displaying the total number of visits to this site since the application was started, at the time when the user's session is started. This is because we fix the total number of visits to the site, when we start the session in global.asa and we can't update it dynamically. We will also be showing the number of active visitors, as well as the time at which the current session began. Even though we are interacting with an application-level variable, we do not need to Lock and Unlock the Application object if we only want to read the data.

 

As you can see from the screen shots, once a session times out, the Session_OnEnd really is called. This can be seen by the change in number of active users as we look at each of the browser snapshots.

 

We've completed our discussions on how to preserve data from page to page using the Session object. Also, we have covered how to keep information from session to session in an application using the Application object.

 

Finally, we'll turn our attention back towards the mechanism that is now supposedly obsolete.

<< 8.4.3- Session Object MethodsChapter88.5.1- Cookies - The End of the Line? >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |