![]() |
||||||
|
[Lifetime of variables][Applications][The
Global.asa file][ASP Built-in Objects][Response
Object] The Global.asa file is an optional file where you can specify event scripts and declare session and application objects that can be accessed by every page in an ASP application. 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. Events 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 are 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 On_Start" 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:
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. Restrictions Restrictions on what you can include in the Global.asa file:
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:
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":
You can also include functions in the Global.asa file. In the example below the Application_OnStart subroutine fires 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 customer’s table. The recordset is assigned to an array, where it can be accessed from 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. The Session_OnStart subroutine adds one to the Application variable named visitors every time a new visitor arrives. The Session_OnEnd subroutine subtracts one from the visitor’s variable each time this subroutine is triggered. The Global.asa file:
To display the number of current visitors in an ASP file:
|
||||||
![]() |
||||||