Programming we already know that the scripts for the OnStart, OnEnd events of application and session objects must be declared in the Global.asa file. What kind of a file is Global.asa? What is the role of it? And how to use it? And listen to me slowly.
First, ASA is the file suffix name, which is the acronym for Active Server application. The Global.asa file can manage two very demanding objects in ASP applications: application, session.
It is actually an optional file where program writers can specify event scripts and declare objects with session and application scopes. The contents of this file are not intended to be displayed to the user, but are used to store event information and objects that are used globally by the application. The file must reside in the root directory of the application. There can be only one Global.asa file per application.
The most common misconception about global.asa files is that it can be used as a stack for general-purpose functions and subroutines. Global.asa files can only be used to create references and capture launches of objects, and to end application objects and session objects.
Global.asa files are accessed primarily based on session-level events and are invoked in the following three cases:
1 when the 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 a Global.asa file.
The standard file format for Global.asa is as follows:
<script language= "VBScript" runat= "Server" >sub Application_OnStart ' Application_OnStart Run the End Sub Sub Session_OnStart ' Session_OnStart when any customer first accesses the home page of the application running End Sub Sub Session_OnEnd ' Se when the customer first runs any of the pages in the ASP application Ssion_onend Run End Sub Sub Application_OnEnd ' Application_OnEnd when a client's session times out or quits the application runs End Sub </script when the Web server for that site is closed >
First, Session_OnStart
Take a look at the code that controls the user's entry into the page:
1,global.asa (placed under the root directory of the virtual directory being debugged)
<script language= "VBScript" runat= "Server" >sub Session_OnStart ' as long as the user first login to this site, you will jump to the home page Response.Redirect ("htp:/ /www.cnbruce.com/") End sub</script>
Then debug any files under the current virtual directory, and you'll notice that all the pages are jumping htp://www.cnbruce.com/
Through this "forced into a page" example, you can imagine that when the home page needs to be attention is very necessary.
The following is an example of an "online number" to continue to observe the Session_OnStart and Session_OnEnd events.
Second, Session_OnEnd
2,global.asa (placed under the root directory of the virtual directory being debugged)
<script language=vbscript runat=server>sub Application_OnStart ' initial value is 0 application ("OnLine") = 0 End Sub Sub session _onstart ' one user access to Count plus 1 application.lock application ("online") = Application ("online") + 1 Application.UnLock End Sub Sub S Ession_onend ' The end of a user process, the count minus 1 (p.s. If the event program is not available, then 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" thensession. Abandon () response.endend if%> Current Total <%=application ("online")%> online <a href= "Online.asp?logout=true" > Exit </a>
You find that there is only one application ("OnLine") in the page, and it is also 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 turn to close the window or exit two methods of debugging.
Third, continue to refine
The above program you will find: when the "exit" connection to close the window and directly close the window effect is not the same. Because the session exists for the time being, when the closed window directly, does not trigger the Session_OnEnd event, so how to achieve this almost impossible idea?
As we all know, when the page is closed can be associated with a onunload event, then the onunload as long as you can perform the cancellation function of the session is not what we need? Needless to say, the online.asp will be modified.
3,online.asp
<%if request.querystring ("logout") = "true" thensession. Abandon () response.endend if%><body onunload=javascript:window.open ("exit.asp") > currently has a total of <%=application (" Online ")%> <a href=" Online.asp?logout=true "> Exit </a>
Note that when the online.asp is onunload, the exit.asp will open, so just set it in exit.asp
Session. Abandon () No, it's OK.
4,exit.asp
<%session. Abandon ()%><script>self.close () </script>
Of course, a script is added, which is to close itself immediately after the cancellation of the session.
So now basically a web application of online statistics is OK.
Iv. in-depth study of Global.asa
From the above debugging, extrapolate you, will definitely ask a question: How to control the number of registered users online?
There are a few files to look at one of the following:
5,global.asa (placed under the root directory of the virtual directory being debugged)
<script language= "VBScript" runat= "Server" >sub application_onstartapplication ("online") =0end SubSub Onstartend SubSub Session_OnEnd If session.contents ("pass") then ' to determine whether to be a logged-on user Session_OnEnd Application.Lock ("Online") =application ("online")-1 Application.UnLock End IfEnd SubSub Application_onendend sub</script>
Because once the user accesses the server regardless of whether the user is logged in, will produce OnStart event, and now need only the user online, so can not onstart events in the Applicaiton ("online" plus 1.
Also because regardless of whether is the logon user's session end will produce the OnEnd event (if has the visitor to visit the server but does not log in, his session will also produce OnEnd event), therefore in Session_ The OnEnd event uses the sentence if statement to determine whether the logged in user's OnEnd event, and if so, the number of people online is reduced by 1.
It is also noteworthy that session.contents ("pass" is used because the session object is not allowed in the OnEnd event, but the session variable can be invoked with a collection of Session objects.) In other words, you cannot write the session directly (pass), but you need to write session.contents (pass).
6,login.asp
<%if request.querystring ("logout") = "true" thensession. Abandon () End Ifsubmitname=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 sessions ("pass") =true else Response.Write "