We already know that the OnStart and OnEnd event scripts of the Application and Session objects must be declared in the Global. asa file. What kind of file is Global. asa? What is its role? How should we use it? Let me hear about it.
First. AsaIs the file suffix, it isActive Server ApplicationThe abbreviation. The Global. asa file can be used to manage two very demanding objects in ASP applications: Application and Session.
It is actually an optional file, where programmers can specify event scripts and declare objects with sessions and application scopes. Content of this fileNot usedShown to the user,InsteadStores event information and objects globally used by applications. This file must be stored in the application'sRoot directory. Each applicationOnly oneThe Global. asa file.
The most common Global. asa FileThe error concept is:It can be used as a library for general functions and subprograms. Global. asa FileCan only be usedCreate an object reference and capture startup, and end the Application object and Session object.
The Global. asa file is accessed Based on session-level events and called in the following three cases:
1. When Application_OnStart or Application_OnEnd event is triggered.
2. When the Session_OnStart or Session_OnEnd event is triggered.
3. When referencing an Object that is instantiated in the Global. asa file ).
The standard File Format of Global. asa is as follows:
<Script language = "VBScript" RUNAT = "Server">
Sub Application_OnStart
'Application _ OnStart is run when any customer accesses the homepage of the Application for the first time.
End Sub
Sub Session_OnStart
'Session _ OnStart: run when the customer runs any page of ASP application for the first time
End Sub
Sub Session_OnEnd
'Session _ OnEnd when a client's Session times out or exits the application
End Sub
Sub Application_OnEnd
'Application _ OnEnd: run when the website's WEB server is closed
End Sub
</SCRIPT> |
I. Session_onStart
First, let's look at a code that controls the user's access to the page:
1. Global. asa (placed under the root directory of the debugged virtual directory)
<Script language = "VBScript" RUNAT = "Server">
Sub Session_OnStart
Response. redirect ("htp: // www.cnbruce.com /")
End Sub
</SCRIPT>
|
After debugging any files in the current virtual directory, you will find that all pages are redirected to htp: // www.cnbruce.com/
In this example, we can imagine that it is necessary to pay attention to the homepage.
Next we will continue to observe the Session_OnStart and Session_OnEnd events with an example of "online users ".
Ii. Session_onEnd
2. Global. asa (placed under the root directory of the virtual directory to be debugged)
<Script language = VBScript RUNAT = Server>
The initial value of Sub Application_onStart is 0.
Application ("OnLine") = 0
End Sub
Sub Session_onStart
Application. Lock
Application ("OnLine") = Application ("OnLine") + 1
Application. Unlock
End Sub
Sub Session_OnEnd: the end of a user process. The count is reduced by 1 (P.S. If the event program does not exist, the page access program is executed .)
Application. Lock
Application ("OnLine") = Application ("OnLine")-1
Application. Unlock
End Sub
</SCRIPT> |
3, online. asp
<%
If request. querystring ("logout") = "true" then
Session. Abandon ()
Response. end
End if
%>
<% = Application ("OnLine") %> OnLine
<A href = "online. asp? Logout = true "> exit </a>
|
You find that there is only one Application ("OnLine") on this page, and it is still displayed as a reference. So where does the value come from? This is the key to the Global. asa file.
You can open a window in sequence on the local machine.Close WindowOrExitTwo Methods for debugging.
III. Further details
In the above program, you will find that the effect of closing the window after the "exit" connection is different from that of closing the window directly. Due to the time of the Session, when the window is closed directly, it cannot trigger the Session_OnEnd event. So how can we implement this idea that is almost impossible?
As we all know, when a webpage is closed, an onunload Event can be associated. Is this onunload required if the Session logout function can be executed? Modify online. asp.
3, online. asp
<%
If request. querystring ("logout") = "true" then
Session. Abandon ()
Response. end
End if
%>
<Body onunload = javascript: window. open ("exit. asp")>
<% = Application ("OnLine") %> OnLine
<A href = "online. asp? Logout = true "> exit </a>
|
Note: When onunload is performed on online. asp, exit. asp will be opened. You only need to set session. Abandon () in exit. asp.
4, exit. asp
<% Session. Abandon () %>
<Script>
Self. close ()
</Script>
|
Of course, a Script is added to close the Session immediately after the Session is canceled.
Now we can use a Web application for online statistics.
4. in-depth research on Global. asa
From the above debugging, you will certainly ask the following question: how to control the number of registered users online?
Let's look at the following files one by one:
5. Global. asa (placed under the root directory of the debugged virtual directory)
<Script language = "VBScript" RUNAT = "Server">
Sub Application_OnStart
Application ("online") = 0
End Sub
Sub Session_OnStart
End Sub
Sub Session_OnEnd
If session. contents ("pass") then', determine whether it is the user's Session_OnEnd
Application. lock
Application ("online") = application ("online")-1
Application. unlock
End if
End Sub
Sub Application_OnEnd
End Sub
</SCRIPT> |
Note that the Session_OnStart block in this Global. asa does not have any events.
Because an OnStart event is generated when a user accesses the server, regardless of whether the user logs in or not, but now all he needs is to log on to the user online. Therefore, Applicaiton ("online") cannot be used in the OnStart event ") add 1.
Besides, an OnEnd event is generated no matter whether the user is logged on to the server or not. (If a visitor accesses the server but does not log on, an OnEnd event is generated after the user's session ends ), therefore, an if statement is used in the Session_OnEnd event to determine whether the event is an OnEnd event of a logged-on user. if so, the number of online users is reduced by 1.
It is worth noting that:Session. contents ("pass ")Because in the OnEnd eventDisable SessionObject, but you can use a set of Session objects to call session variables. That is to say, you cannot directly write session ("pass"), but you need to write session. contents ("pass ").
6. login. asp
<%
If request. querystring ("logout") = "true" then
Session. Abandon ()
End if
Submitname = request. form ("submit ")
If submitname = "submit" then
Name = request. form ("name ")
Pwd = request. form ("pwd ")
If name = "cnbruce" and pwd = "cnrose" then
Session ("name") = name
Session ("pass") = true
Else
Response. write "Error Name Or Pwd. <a href = 'login. asp '> Back </a>"
Response. end
End if
Application. lock
Application ("online") = application ("online") + 1
Application. unlock
%>
Currently registered member <% = application ("online") %>.
<A href = "login. asp? Logout = true "> exit </a>
<% Else %>
<Form action = "login. asp" method = "post">
<Input type = "text" name = "name"> <br>
<Input type = "password" name = "pwd"> <br>
<Input type = "submit" name = "submit" value = "submit">
<% End if %>
|
When the password is cnrose, a session ("pass") = true is generated, which is determined in Global. asa.
5. Continue to use your imagination
Think about it. It is not enough to count the number of online users, but to determine the online status of users.
As you can imagine, when a user logs on. in asp, Set online to 1 (if a database exists, upload), but offline users set online to 0. To improve it, modify the Session_OnEnd event, in this event, Set online to 0 (the same value as 0 will be uploaded )......
Of course, Global. asa is far more than that. But now we don't have to worry about it all. When we come into contact with the database, we will return to see it and continue to study the file. I believe that at that time we will surely feel a lot. So, let's get through the above :)