ASP  «Prev  Next»

Lesson 4The Global.asa file: ASP's Information Resource
ObjectiveCreate a file for session start and end procedures.

Global.asa file: ASP's Information Resource

Sometimes you will want to create objects, such as a user session log, and variables, such as an array to hold user preferences, that are available throughout a user's session.
You will want to initialize those objects and maybe work with some when a session starts.
Now you have a circular problem: you want to initialize a Session variable and you want to do so before the session begins.
ASP's solution to this is to check a file named Global.asa when a new user begins a session with the Web site and when a user session terminates.
Global.asa is an optional file that you create and save in the root directory of your Web application. If you have defined a session-starting procedure and/or a session-ending procedure in this global.asa file, ASP will execute this code at the appropriate time.
The procedures in Global.asa related to sessions are events named Session_OnStart and Session_OnEnd.

Session_OnStart

The Session_OnStart event is executed every time a new user begins a session with the web site. This event can be useful in setting up the user environment through Session scoped variables. Here is an example of how the Session_OnStart event can be implemented in the Global.asa file. Our goal is to record the date and time a new user first contacts our Web site:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart ( )
   ....
   ....
   Session("VisitStarted") = Now()
   Session("VisitorID") = intTotalVisitors + 1
End Sub
</SCRIPT>

Session_OnEnd

The Session_OnEnd event occurs when a session terminates. A user session can terminate as a result of the session timing out or if it is explicitly abandoned. The Session_OnEnd event is very useful for executing code that transfers user information to a text file or database.
Here is an example of how Session_OnEnd might be implemented in Global.asa.

Filename: GLOBAL.ASA
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
Session("VisitStarted") = Now()
Session("VisitorID") = intTotalVisitors + 1
End Sub
Sub Session_OnEnd
Dim strLog
StrLog = Session("VisitorId") + Session("ShoppingCartItems") + Now()
objLogfile.WriteLine strLog   'write user information to a text file
End Sub
</SCRIPT>

Because the Global.asa. is used to configure your application on your Web server, it is executed on the Server, hence the RUNAT=SERVER.
Question: When the session is completed, either by the user or by the web server, which event fires?
Answer: Sesson_OnEnd.
Question: An individual session is started when a user browses a page on the web site. When does the session end?
Answer: A session ends when the user's session times out or when they shut down their browser.

Timeout

The Timeout property of the Session object determines how long a user session can remain idle before it is terminated. If a session is allowed to continue for idle users, overall server performance will degrade. Normally the Session.Timeout property is set in the Session_OnStart event. You can set the Timeout value as shown below:
<% Session.Timeout = 30 %>
This will limit a session idle time to 30 minutes. If the user remains idle for more than 30 minutes, the session will terminate and free up resources for the next visitor. If Session.Timeout property isn't specified, the default of 20 minutes will automatically be set.
The next lesson describes some ways to personalize Web sites.