We already know that scripts for the OnStart and OnEnd events of the Application and Session objects must be declared in the Global.asa file. So what kind of file is Global.asa? What is its function? How should I use it? Let me tell you slowly.
First of all, .asa is the file suffix name, which is the abbreviation of Active Server Application. The Global.asa file can manage two very demanding objects in ASP applications: Application and Session.
It is actually an optional file in which the program writer can specify event scripts and declare objects with session and application scopes. The contents of this file are not used to display to the user, but to store event information and objects used globally by the application. This file must be stored in the root directory of the application. Each application can only have one Global.asa file.
The most common misconception about Global.asa files is that it can be used as a library for general use functions and subroutines. The Global.asa file can only be used to create references and capture startups of objects, as well as end Application objects and Session objects.
The Global.asa file is accessed based on session-level events and is called in the following three cases:
1. When the Application_OnStart or Application_OnEnd event is triggered.
2. When the Session_OnStart or Session_OnEnd event is triggered.
3. When referring to an object (Object) that is instantiated in the Global.asa file.
The standard file format of Global.asa is as follows:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Application_OnStart 'Application_OnStart Runs when any customer first accesses the homepage of the application End Sub Sub Session_OnStart 'Session_OnStart Runs when the client first runs any page in the ASP application End Sub Sub Session_OnEnd 'Session_OnEnd Runs when a client's session timed out or exits the application End Sub Sub Application_OnEnd 'Application_OnEnd Runs when the site's WEB server is down End Sub </SCRIPT> |
1. Session_onStart
Let’s first look at a code that controls users to enter the page:
1. Global.asa (placed under the root directory of the debugged virtual directory)
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Session_OnStart 'As long as the user logs into this site for the first time, he will jump to the homepage response.redirect("htp://www.cnbruce.com/") End Sub </SCRIPT> |
Then debug any files in the current virtual directory, and you will find that all pages jump to http://www.cnbruce.com/
Through this example of "forced entry into a certain page", it can be imagined that it is very necessary when the home page needs to be followed.
Let’s continue to observe the Session_OnStart and Session_OnEnd events with an example of “number of online users”
2. Session_onEnd
2. Global.asa (placed under the root directory of the debugged virtual directory)