Introduction to ASP Programming (10): Global.asa File _asp Foundation

Source: Internet
Author: User
We already know that the scripts for the OnStart, onend events of the 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 of all . Asais the file suffix name, which is Active Server ApplicationAcronym for the first letter. 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 the file not used toShown to the user, but toStores event information and objects that are used globally by the application. The file must be stored in the application's root directoryWithin Each application can only have oneGlobal.asa files.

The most common about global.asa files the wrong concept isIt can be used as a stack for commonly used functions and subroutines. Global.asa file can only be used forCreate references and capture initiation of objects, and 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 when any customer first accesses the first page of the application
End Sub

Sub Session_OnStart
' Session_OnStart run when a customer first runs any page in an ASP application
End Sub

Sub Session_OnEnd
' Session_OnEnd run when a client's session times out or quits the application
End Sub

Sub Application_OnEnd
' Application_OnEnd run when the Web server for the site is down
End Sub
</SCRIPT>



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 ' A user access to count plus 1
Application.Lock
Application ("online") = Application ("online") + 1
Application.UnLock
End Sub

Sub Session_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" Then
Session. Abandon ()
Response.End
End 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 this machine in turn to do Close WindowOr ExitDebugging of two methods.

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? Cut the crap, change the online.asp

3,online.asp


<%
If Request.QueryString ("logout") = "true" Then
Session. Abandon ()
Response.End
End If
%>
<body Onunload=javascript:window.open ("exit.asp") >
Current Total <%=application ("online")%> online
<a href= "Online.asp?logout=true" > Exit </a>



Note that when the online.asp is onunload, the exit.asp is opened, so as long as the Session.Abandon is set in the exit.asp () it is 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_OnStart
Application ("online") =0
End Sub

Sub Session_OnStart
End Sub

Sub Session_OnEnd
If Session.Contents ("pass") then ' to determine whether to be a logged-on 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 do any events.

Because once a user accesses the server regardless of whether the user is logged in, will produce OnStart events, and now need only to log in the user's online, so can not be in the OnStart event to make 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.

And it's worth noting: session.contents ("Pass")The use because in the OnEnd incident prohibit use of sessionObject, but you can use a collection of Session objects to invoke the session variable. 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" 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")%> person.
<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%>


Simply detect the use of the name is Cnbruce, the password is cnrose, the generation of a session ("pass") =true, is placed in the Global.asa judge.

Five, continue to play the imagination
Think about it, think again. It is not enough to count how many people are online, but to judge the online status of the user.
Can imagine the basic, when the user login, in the Login.asp will be online set to 1 (if there is a database upload), but the user offline will be online set to 0, to improve it, it is necessary to modify the Session_OnEnd event, In this event, set the online to 0 (the same value will be uploaded for 0) ...

Of course, Global.asa is much more than that. However, we do not worry about all the grasp, when we contact the database and then return to look, continue to study the document, I believe that then will feel a lot. So, first of all, the above to thoroughly grasp it:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.