ASP Global.asa File

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods accessible to each page in the ASP application.

Global.asa File

The Global.asa file is an optional file that can contain declarations of objects, variables, and methods accessible to each page in the ASP application. All valid browser scripts can be used in Global.asa.

The Global.asa file can include the following contents:

  • Application Event
  • Session Event
  • <object> declaration
  • TypeLibrary Declaration
  • #include Directive

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.

Event in Global.asa

In Global.asa, we can inform the application and session objects of what to do at startup and shutdown. The code that performs this task is placed in the 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 of the ASP application. This event occurs when the web server restarts or the Global.asa file is edited. The 'Session_OnStart' event occurs immediately after this event.

Session_OnStart - This event occurs each time a new user requests their first page in the ASP application.

Session_OnEnd - This event occurs each time a user ends their session. If no page is requested within a specified time (the default event is 20 minutes), the session will end.

Application_OnEnd - This event occurs after the last user ends their session. A typical scenario is that this event occurs when the Web server stops. This subroutine is used to clear settings after the application stops, such as deleting records or writing information to a text file.

The Global.asa file may look like this:

<script language="vbscript" runat="server">
sub Application_OnStart
  'some code
end sub
sub Application_OnEnd
  'some code
end sub
sub Session_OnStart
  'some code
end sub
sub Session_OnEnd
  'some code
end sub
</script>

Note:Since we cannot insert scripts in the Global.asa file using ASP script delimiters (<% and %>), we need to use the HTML <script> element.

<object> declaration

Objects with session or application scope can be created in the Global.asa file by using the <object> tag.

Note:The <object> tag should be located outside the <script> tag.

Syntax:

<object runat="server" scope="scope" id="id"
{progid="progID"|classid="classID"}>
....
</object>
Parameter Description
scope Set the scope (scope) of the object (Session or Application).
id Assign a unique id to the object.
ProgID

The id associated with ClassID. The format of ProgID is: [Vendor.]Component[.Version].

The ProgID or ClassID must be specified.

ClassID

Specify a unique id for the COM class object.

The ProgID or ClassID must be specified.

instance

The first instance creates a named "MyAd" session scope object using the ProgID parameter:

<object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator">
</object>

The second instance creates a named "MyConnection" using the ClassID parameter:

<object runat="server" scope="application" id="MyConnection"
classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21">
</object>

The objects declared in this Global.asa file can be used by any script in the application.

GLOBAL.ASA:

<object runat="server" scope="session" id="MyAd" progid="MSWC.AdRotator">
</object>

You can reference this "MyAd" object from any page in the ASP application:

A .ASP file:

<%=MyAd.GetAdvertisement("/banners/adrot.txt")%> 

TypeLibrary Declaration

TypeLibrary (Type Library) is a container that holds the DLL files corresponding to COM objects. By including a call to the TypeLibrary in Global.asa, you can access the constants of COM objects, and ASP code can also report errors more effectively. If your application site's application depends on COM objects that have already declared data types in the type library, you can declare the type library in Global.asa.

Syntax:

<!--METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->
Parameter Description
file Specifies the absolute path to the type library. The parameters file or uuid are required, either one cannot be missing.
uuid Specifies a unique identifier for the type library. The parameters file or uuid are required, either one cannot be missing.
version Optional. Used to select a version. If the specified version is not found, the closest version will be used.
lcid Optional. Used as the locale identifier for the type library.

Error value

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 Type library cannot be loaded
ASP 0225 Type library cannot be wrapped

Note:The METADATA tag can be placed anywhere in the Global.asa file (both inside and outside the <script> tag). However, we still recommend placing 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 the text in 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.

How to use subroutines

Global.asa is often used to initialize variables.

The following example demonstrates how to detect the exact time when a visitor first arrives at the 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:

<script language="vbscript" runat="server">
sub Session_OnStart
Session("started")=now()
end sub
</script>

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 example, to the page "newpage.asp":

<script language="vbscript" runat="server">
sub Session_OnStart
Response.Redirect("newpage.asp")
end sub
</script>

We can also include functions in Global.asa.

In the following example, when the Web server starts, the Application_OnStart subroutine is also started. Subsequently, the Application_OnStart subroutine calls another subroutine named "getcustomers". The "getcustomers" subroutine opens a database and then retrieves a record set from the "customers" table. This record set is assigned to an array, so that any ASP page can access this array without querying the database:

<script language="vbscript" runat="server">
sub Application_OnStart
getcustomers
end sub
sub getcustomers 
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs=conn.execute("select name from customers")
Application("customers")=rs.GetRows
rs.Close
conn.Close
end sub
</script>

Global.asa instance

In this example, we will create a Global.asa file that can calculate the current number of visitors.

Application_OnStart sets the value of the Application variable "visitors" to 0 when the server starts.

Every time a new user visits, the Session_OnStart subroutine will add 1 to the variable "visitors".

This subroutine will subtract 1 from the variable "visitors" every time the Session_OnEnd subroutine is triggered.

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>

This ASP file will display the number of current users:

<html>
<head>
</head>
<body>
<p>There are <%response.write(Application("visitors"))%> online now!</p>
</body>
</html>