YouTip LogoYouTip

Asp Globalasa

ASP Global.asa |


Global.asa File

A Global.asa file is an optional file that can contain declarations for objects, variables, and methods accessed by every page in an ASP application.

All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used in Global.asa.

A Global.asa file can only contain the following content:

  • Application Events
  • Session Events
  • <object> Declarations
  • TypeLibrary Declarations
  • #include Directives

Note: A Global.asa file must be stored in the root directory of the ASP application, and each application can only have one Global.asa file.


Events in Global.asa

In Global.asa, you can tell the application and session objects what to do when an application/session starts and what to do when an application/session ends. The code that accomplishes this task is placed in event handlers. A 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 after the Web server restarts or after the Global.asa file is edited. The "Session_OnStart" event occurs immediately after this event.

Session_OnStart - This event occurs whenever a new user requests his/her first page in the ASP application.

Session_OnEnd - This event occurs whenever a user ends a session. If the user does not request any pages within a specified time period (default is 20 minutes), the user session will end.

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 clear settings after the application stops, such as deleting records or writing information to a text file.

A Global.asa file might 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 using ASP script delimiters (<% and %>) in a Global.asa file, we need to place the subroutines inside the HTML <script> element.


<object> Declaration

You can create objects with session or application scope in a Global.asa file by using the <object> tag.

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

Syntax

<object runat="server" scope="_scope_" id="_id_" {progid="_progID_"|classid="_classID_"}>

 ....

</object>
ParameterDescription
scopeSets the scope (Session or Application) of the object.
idAssigns a unique id to the object.
ProgIDAn id associated with the ClassID. The format of ProgID is: [Vendor.]Component[.Version]. Either ProgID or ClassID must be specified.
ClassIDAssigns a unique id to the COM class object. Either ProgID or ClassID must be specified.

Example

The first example creates a session-scoped object named "MyAd" by using the ProgID parameter:

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

</object>

The second example creates an application-scoped object named "MyConnection" by using the ClassID parameter:

<object runat="server" scope="application" id="MyConnection"

 classid="Clsid:8AD3067A-B3FC-11CF-A560-00A0C9081C21">

</object>

Objects declared in a 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 the "MyAd" object from any page in the ASP application:

An ASP file:

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

TypeLibrary Declaration

A TypeLibrary is a container that holds DLL files corresponding to COM objects. By including a reference to a TypeLibrary in the Global.asa file, you can access constants of COM objects, and ASP code can report errors better. If your Web application relies on COM objects that use data types declared in a type library, you can declare the type library in Global.asa.

Syntax

<!--METADATA TYPE="TypeLib"

 file="_filename_" uuid="_id_" version="_number_" lcid="_localeid_"

 -->
ParameterDescription
fileSpecifies the absolute path pointing to the type library. Either the file parameter or the uuid parameter is required.
uuidSpecifies the unique identifier of the type library. Either the file parameter or the uuid parameter is required.
versionOptional. Used to select the version. If the required version is not found, the closest version will be used.
lcidOptional. Locale identifier for the type library.

Error Values

The server will return one of the following error messages:

Error CodeDescription
ASP 0222Invalid Type Library Specification
ASP 0223Type Library Not Found
ASP 0224Unable to Load Type Library
ASP 0225Unable to Wrap Type Library

Note: The METADATA tag can appear anywhere in the Global.asa file (inside or 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 a Global.asa file:

  • You cannot display the text in a Global.asa file. This file cannot display information.
  • You can only use the Server and Application objects in 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 objects.

How to Use 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. Time is stored in the 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 case, to a page named "newpage.asp":

<script language="vbscript" runat="server">

 sub Session_OnStart

 Response.Redirect("newpage.asp")

 end sub

</script>

You can include functions in a 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 then retrieves a recordset from the "customers" table. This recordset is assigned to an array, which any ASP page can access 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 current number of 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 variable "visitors" by 1.
  • Whenever the Session_OnEnd subroutine is triggered, it decrements the variable "visitors" by 1.

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>

In an ASP file, display the number of current visitors:

<!DOCTYPE html>

<html>

<head>

</head>

<body>

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

</body>

</html>
← Asp Send EmailAsp Incfiles β†’