Global.asa File
Global.asa file is an optional file that can contain declarations of objects, variables, and methods that are accessible to every page in an ASP application.
All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used in Global.asa.
The Global.asa file can only contain the following:
- Application events
- Session events
- <object> declarations
- TypeLibrary declarations
- #include directives
Note: The Global.asa file must be placed in the root directory of the ASP application, and each application can have only one Global.asa file.
Events in Global.asa
In Global.asa, you can instruct the application and session objects on what to do when the application/session starts and ends. The code to accomplish this task is placed in event handlers. The Global.asa file can contain four types of events:
Session_OnStart - This event occurs whenever a new user requests their first page in the ASP application.
Session_OnEnd - This event occurs whenever a user ends their session. A user's session ends if they do not request any pages within a specified time (default is 20 minutes).
Application_OnEnd - This event occurs after the last user ends their session. Typically, this event occurs when the web server stops. This subroutine is used to clean up settings after the application stops, such as deleting records or writing information to a text file.
A Global.asa file might look like this:
Note: Since we cannot use ASP script delimiters (<% and %>) to insert scripts in the Global.asa file, we need to place subroutines inside the HTML <script> element.
<object> Declarations
You can create objects with session or application scope in the Global.asa file using the <object> tag.
Note: The <object> tag should be outside the <script> tag!
Syntax
Parameter | Description |
---|---|
scope | Sets the scope (Session or Application) of the object. |
id | Specifies a unique id for the object. |
ProgID | The id associated with the ClassID. The format of ProgID is: [Vendor.]Component[.Version]. Either ProgID or ClassID must be specified. |
ClassID | Specifies a unique id for the COM class object. Either ProgID or ClassID must be specified. |
Example
The first example creates a session-scoped object named "MyAd" using the ProgID parameter:
The second example creates an application-scoped object named "MyConnection" using the ClassID parameter:
Objects declared in the Global.asa file can be used by any script in the application:
TypeLibrary Declarations
A TypeLibrary is a container that holds the DLL file corresponding to a COM object. By including a call to the TypeLibrary in the Global.asa file, you can access constants of the COM object, and ASP code can better report errors. If your web application relies on COM objects whose data types are declared in a type library, you can declare the type library in Global.asa.
Syntax
Parameter | Description |
---|---|
file | Specifies the absolute path to the type library. Either the file parameter or the uuid parameter is required. |
uuid | Specifies the unique identifier for the type library. Either the file parameter or the uuid parameter is required. |
version | Optional. Used to select a version. If the desired version is not found, the closest version will be used. |
lcid | Optional. The locale identifier for the type library. |
Error Values
The server will return one of the following error messages:
Error Code | Description |
---|---|
ASP 0222 | Invalid type library specification |
ASP 0223 | Type library not found |
ASP 0224 | Unable to load type library |
ASP 0225 | Unable to wrap type library |
Note: The METADATA tag can appear anywhere in the Global.asa file (inside or outside the <script> tag). However, it is recommended to place the METADATA tag at the top of the Global.asa file.
Restrictions
Restrictions on what can be referenced in the Global.asa file:
- You cannot display text from the Global.asa file. This file cannot display information.
- You can only use the Server and Application objects in the Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use the Server, Application, and Session objects. In the Session_OnStart subroutine, you can use any built-in objects.
Using Subroutines
Global.asa is often used to initialize variables.
The following example demonstrates how to detect the exact time a visitor first arrives at the web site. The time is stored in a Session object named "started", and the value of the "started" variable can be accessed by any ASP page in the application:
Global.asa can also be used to control page access.
The following example demonstrates how to redirect each new visitor to another page, in this case to a page named "newpage.asp":
You can include functions in the Global.asa file.
In the following example, when the web server starts, the Application_OnStart subroutine also starts. Then, the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and retrieves a recordset from the "customers" table. This recordset is assigned to an array, which can be accessed by any ASP page without querying the database:
Global.asa Example
In this example, we will create a Global.asa file that counts the number of current visitors.
- When the server starts, Application_OnStart sets the Application variable "visitors" to 0.
- Whenever a new visitor arrives, the Session_OnStart subroutine increments the "visitors" variable by 1.
- Whenever the Session_OnEnd subroutine is triggered, it decrements the "visitors" variable by 1.
Global.asa file:
In the ASP file, display the number of current visitors: