In ASP, the Global.asa file is an optional file that is generally used to declare application and/or session variables that can be accessed by every page in an ASP
application. The Global.asa file is stored in the root directory of the ASP application. You can only have one Global.asa file.
Global.asa Events
In Global.asa you can instruct the web server as to what to do when the ASP application starts and ends, as well as when a user session starts and ends.
The start and end events for the application and session are handled by the following four types of events:
- Application_OnStart
This event occurs when the first visitor calls the very first page in an ASP application. This event occurs after the Web server is restarted
or after the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.
- Session_OnStart
This event occurs every time a new visitor requests his or her first page in the ASP application.
- Session_OnEnd
This event occurs every time a user session ends. The session ends after a timeout period of inactivity. By default, the value is 20 minutes.
- Application_OnEnd
This event occurs after the last user session has ended, such as when the web server service has stopped.
Here is an example of how to use the Global.asa file to track the number of online visitors:
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("users") = 0
Application("subtotal") = 0
Application("total") = 0
End Sub
Sub Application_OnEnd()
Application("total") = Application("subtotal")
End Sub
Sub Session_OnStart
Application.Lock
Application("users") = Application("users") + 1
Application("subtotal") = Application("subtotal") + 1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("users") = Application("users") - 1
Application.UnLock
End Sub
</script>
Display the number of online users in your ASP page:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<p>There are <% Response.Write(Application("users")) %> online now!</p>
</body>
</html>
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources