The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that are accessed by each page in an ASP application. All legitimate browser scripts can be used in Global.asa.
The Global.asa file can contain only application events ,Session events, <object> statements ,typelibrary declarations , #include instructions to these kinds of content.
The Global.asa file must be placed in the root directory of the ASP application, and each application can have only one global.asa file.
Events in the Global.asa
The Global.asa file should contain 4 Sub procedures, and their names are fixed so that IIS can automate these processes when processing ASP scripts, and a standard global.asa code structure is as follows:
<Scriptlanguage= "VBScript"runat= "Server">SubApplication_OnStart ()'some codeEnd SubSubApplication_OnEnd ()'some codeEnd SubSubSession_OnStart ()'some codeEnd SubSubSession_OnEnd ()'some codeEnd Sub</Script>
First, instead of including the code with the <%%> delimiter, the code is included in the <script> tag and the error is denied.
Application_OnStart This event is triggered when the entire Web site is first opened, after the Web server restarts or the Global.asa file is edited. The Session_OnStart event occurs immediately after this event occurs.
The Session_OnStart event occurs whenever a new user requests the first page of a Web site's ASP program. That is, IIS is triggered when assigning SessionID to users.
The Session_OnEnd event occurs every time the user ends the Session. This is related to the setting of the Session.Timeout, within the specified time (the default time is 20 minutes) if the user does not request any page, the user session will end, the Session_OnEnd event will also be triggered.
The Application_OnEnd event occurs after the last user ends its Session. Typically, the WEB server shuts down or restarts, or the site is shut down in IIS manager.
Qualified
Application_OnStart, Application_OnEnd, Session_OnStart, Session_OnEnd four event subroutines can be used in the object, see the following table
|
Server Object |
Application Object |
Session Object |
Any other built-in object |
Application_OnStart |
√ |
√ |
|
|
Application_OnEnd |
√ |
√ |
|
|
Session_OnStart |
√ |
√ |
√ |
√ |
Session_OnEnd |
√ |
√ |
√ |
|
Making online population counters with Global.asa
We use the Session event feature in the Global.asa file to implement a counter for the number of people online, and when each user first accesses the site, we increase the current number of people saved in application, and when the user leaves the site and the Session times out, we The current number of people saved in application is reduced.
The following is the source code in the Global.asa file:
<Scriptlanguage= "VBScript"runat= "Server">SubApplication_OnStart () application ("Visitors") = 0End SubSubApplication_OnEnd ()End SubSubSession_OnStart () Application.Lock () application ("Visitors") =Application ("Visitors") + 1Application.UnLock ()End SubSubSession_OnEnd () Application.Lock () application ("Visitors") =Application ("Visitors") - 1Application.UnLock ()End Sub</Script>
We mainly use the Session_OnStart, Session_OnEnd and Application_OnStart three events here.
- The code in Application_OnStart causes the visitor in application to be initialized when the Web site is opened to 0.
- The code in Session_OnStart makes the visitor in application Add 1.
- The code in Session_OnEnd makes the visitor in application minus 1.
Place this global.asa at the root of the site, and then you can access the value of application Visitor in your ASP file, as in the following code:
<%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%><%Dimnumvisitorsnumvisitors=Application.Contents ("Visitors") Session.Timeout= 1%><!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>Global.asa Statistics Online number</title></Head><Body><H3>Current number of people online:<%=numvisitors%></H3></Body></HTML>
Sample code Download
Global_visitors.rar
Getting Started with ASP (15)-Global.asa