The Global.asa file is an optional file that can contain declarations of objects, variables, and
methods that can be accessed by every page in an ASP application.
The Global.asa file can contain only the following:
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.
In Global.asa 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 in event handlers. The Global.asa file can contain four types of events:
Session_OnStart - This event occurs EVERY time a
NEW user requests his or her first page in the ASP application.
Session_OnEnd - This event occurs EVERY time a user ends a session. A user-session ends
after a page has not been requested by the user for a specified time (by default this is 20
Application_OnEnd - This event occurs after the LAST user has ended the session.
Typically, this event occurs when a Web server stops. This procedure is used to clean up
settings after the Application stops, like delete records or write information to text files.
A Global.asa file could look something like this:
Note: Because we cannot use the ASP script delimiters (<% and %>) to insert scripts
in the Global.asa file, we put subroutines inside an HTML <script> element.
It is possible to create objects with session or application scope in Global.asa by using the
Note: The <object> tag should be outside the <script> tag
Either ProgID or ClassID must be specified.
Either ProgID or ClassID must be
The first example creates an object of session scope named "MyAd" by using the ProgID
The second example creates an object of application scope named "MyConnection" by using
the ClassID parameter:
The objects declared in the Global.asa file can be used by any script in the application:
A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By
including a call to the TypeLibrary in the Global.asa file, the constants of the COM object can
be accessed, and errors can be better reported by the ASP code. If your Web application relies
on COM objects that have declared data types in type libraries, you can declare the type
libraries in Global.asa.
Either the file parameter or the uuid
parameter is required
Either the file parameter or the
uuid parameter is required
The server can return one of the following error messages:
Note: METADATA tags can appear anywhere in the Global.asa file (both inside and outside
<script> tags). However, it is recommended that METADATA tags appear near the top of the
Restrictions on what you can include in the Global.asa file:
Global.asa is often used to initialize variables.
The example below shows how to detect the exact time a visitor first arrives on a Web site. The
time is stored in a Session variable named "started", and the value of the "started"
variable can be accessed from any ASP page in the application:
Global.asa can also be used to control page access.
The example below shows how to redirect every new visitor to another page, in this case to a page
And you can include functions in the Global.asa file.
In the example below the Application_OnStart subroutine occurs when the Web server starts. Then
the Application_OnStart subroutine calls another subroutine named "getcustomers". The
"getcustomers" subroutine opens a database and retrieves a record set from the "customers"
table. The record set is assigned to an array, where it can be accessed from any ASP page
without querying the database:
In this example we will create a Global.asa file that counts the number of current visitors.
The Global.asa file:
To display the number of current visitors in an ASP file:
Your Query was successfully sent!