HOME

ASP Tutorial
ASP HOME
ASP Introduction
ASP Install
ASP Syntax
ASP Variables
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa

ASP Objects
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error

ASP FileSystem
ASP TextStream
ASP Drive
ASP File
ASP Folder
ASP Dictionary

ASP Components
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator

ASP Quick Ref

Examples/Quiz
ASP Examples
ASP Quiz Test

ADO
ADO Introduction

Resources
ASP Resources

Please visit our sponsors !

ASP The Global.asa file

previous next

In Global.asa you can specify event scripts and declare session and application objects that can be accessed by every page in an ASP application.


The Global.asa file

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. All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa.

Global.asa files can contain only the following:

  • Application events
  • Session events
  • <object> declarations 
  • TypeLibrary declarations

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. If you modify the Global.asa file at a later point, you might need to restart the server to make the changes work.


Standard Events in Global.asa

In the Global.asa file 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 into 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 from an ASP application. This event occurs after the Web server is restarted or after the Global.asa file is edited. When this procedure is complete, the "Session_OnStart" procedure runs.

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

Session_OnEnd - This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specified time (by default this is 20 minutes).

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.

You can create a subroutine to handle each of these events in the Global.asa file:

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

Note: We do not use the ASP script delimiters, <% and %>, to insert scripts in the Global.asa file, we have to put the subroutines inside the HTML <script> element.


<object> Declarations

It is possible to create objects with session or application scope in Global.asa by using the <object> tag.

This tag is outside of any <script> tags.

Syntax

<object runat="server" scope=scope id=identifier
{progid="progID"|classid="classID"}>
....
</object>

Parameter Description
scope Sets the scope of the object (either Session or Application)
id Specifies a name for the object
ProgID An id associated with a class id. The format for ProgID is [Vendor.]Component[.Version]

Either ProgID or ClassID must be specified.

ClassID Specifies a unique id for a COM class object.

Either ProgID or ClassID must be specified.

Examples

Example 1

Create an object of session scope named MyAd by using the ProgID parameter:

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

Example 2

Creates an object of application scope named MyConnection by using the ClassID parameter:

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

Example 3

The objects declared in the 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 could reference the object MyAd from any page in the application:

SOME .ASP FILE:

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


TypeLibrary Declarations

A TypeLibrary is a container for the contents of a DLL file corresponding to a COM object. By including the call to the TypeLibrary in the GLOBAL.ASA file, the constants of that 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.

Syntax

<!--METADATA TYPE="TypeLib"
file="filename"
uuid="typelibraryuuid"
version="versionnumber"
lcid="localeid"
-->

Parameter Description
file Specifies an absolute path to a type library.

Either the file parameter or the uuid parameter is required

uuid Specifies a unique identifier for the type library.

Either the file parameter or the uuid parameter is required

version Optional. Used for selecting version. If the requested version is not found, then the most recent version is used
localeid Optional. The locale identifier to be used for the type library

Error Values

The server can 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 tags can appear anywhere inside of the Global.asa file (both inside and outside the <script> tags), however it is recommended that METADATA tags appear near the top of the Global.asa file.


Restrictions

Restrictions on what you can include in the Global.asa file:

  • You can not display text that is written in the Global.asa file. This file can't display information
  • You can not use the #include directive in Global.asa
  • You can only use Server and Application objects in the Application_OnStart and Application_OnEnd subroutines. In the Session_OnEnd subroutine, you can use Server, Application, and Session objects. In the Session_OnStart subroutine you can use any built-in object

How to use the Subroutines

The Global.asa file is often used to initialize variables. 

The example below shows how to detect the exact time a visitor first arrives at your Web site. The time is stored in a Session variable named started, and the value of that variable can be accessed from any ASP page in the application:

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

The Global.asa file 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 called "newpage.asp":

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

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 set of records from the customers table. The recordset is assigned to an array, where it can be accessed from any ASP page 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 Example

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

  • The Application_OnStart sets the Application variable visitors to 0 when the server starts
  • The Session_OnStart subroutine adds one to the variable visitors every time a new visitor arrives
  • The Session_OnEnd subroutine subtracts one from visitors each time this subroutine is triggered

The 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>

To display the number of current visitors in an ASP file:

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


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

Domain Name Registration
Domain Name
Registration & More!



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search