Maintaining Session State With ASP - The Global.asa File
(Page 4 of 5 )
The global.asa file is an optional file where you can specify event scripts and declare session and application objects that can be accessed by every page in your ASP application, as we saw in the example on the previous page.
Note: The global.asa file must be stored in the root directory of the ASP application and each application can only have one global.asa file.
Standard Events in Global.asa In the global.asa file you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed into event handlers. The global.asa file can contain four types of events:
- Application_OnStart: This event occurs when the FIRST user calls the first page from an ASP application. This event also occurs after the web server is restarted or after the global.asa file is edited. When this procedure is complete, the "Session_OnStart" procedure runs.
- Session_OnStart: This event occurs EVERY time a user visits your web site and requests the first page.
- Session_OnEnd: This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specific amount of time (this is 20 minutes by default).
- Application_OnEnd: This event occurs after the LAST user has ended the session. Typically, this event occurs when a web server is stopped/restarted. This procedure is used to clean up settings after the application stops, such as deleting records or writing log information to text files.
Example: A global.asa file with empty event scripts would look like this:
<script language=vbscript runat=server>
SUB Application_OnStart
END SUB
SUB Application_OnEnd
END SUB
SUB Session_OnStart
END SUB
SUB Session_OnEnd
END SUB
</script> In this example we will create a global.asa file that counts the number of current visitors:
- The Application_OnStart sets the Application variable visitors to 0 when the server starts
- The Session_OnStart subroutine adds one to the variable visitors every time a new visitor arrives
- The Session_OnEnd subroutine subtracts one from visitors each time this subroutine is triggered
The Global.asa file:
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub
</script> To display the number of current visitors in an ASP file:
<html>
<head>
</head>
<body>
<p>
There are <%response.write(Application("visitors"))%>
online now!
</p>
</body>
</html> Maintaining Client State With Cookies If a visitor comes to your site and types his name into a form then you might want to remember that information. You may only want to remember it for his current visit (session), or for all subsequent visits for personalization ( i.e. "Welcome back Mike"). The term state describes all client browser data for the session. ASP uses client browser side cookies to remember (or persist) this data.
Cookies are small text files stored on the visitors PC, usually in %root% \ Windows \ Temporary Internet Files \ Cookie:user_ name@Host_Name file. These text files are editable with notepad or Microsoft Word. Two types of information are stored in the cookie files:
- Client Data: The variables that make up the cookie for the visitor. These are stored as name/value pairs, such as name=john.
- Unique Cookie ID: This allows the ASP Session to identify the client browser on a page to page and visit to visit basis.
Example: <%
Response.Cookies("myCookie")("myValue1") = 1
%> ... and then to retrieve the value of the cookie:
<% =Request.Cookies("myCookie")("myValue1") %>Next: Conclusion >>
More ASP Articles
More By Himanshu Khatri