First of all, .asa is the file suffix, which is the abbreviation of Active Server Application. The Global.asa file can manage two very demanding objects in ASP applications: Application and Session.
It is actually an optional file in which program writers can specify event scripts and declare objects with session and application scope. The contents of this file are not used for display to the user, but are used to store event information and objects used globally by the application. This file must be placed in the root directory of the application. There can only be one Global.asa file per application.
The most common misconception about the Global.asa file is that it can be used as a library for commonly used functions and subroutines. The Global.asa file can only be used to create object references and capture starts, and to end Application objects and Session objects.
The Global.asa file is mainly accessed based on session-level events and is called in the following three situations:
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 (Object) 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 Runs when any customer first visits the home page of the application
End Sub
SubSession_OnStart
'Session_OnStart runs when the client first runs any page in the ASP application
End Sub
SubSession_OnEnd
'Session_OnEnd Runs when a client's session times out or exits the application
End Sub
SubApplication_OnEnd
'Application_OnEnd runs when the WEB server of the site is shut down
End Sub
</SCRIPT>
1. Session_onStart
Let’s first look at a code that controls users to enter 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 logs in to this site for the first time, he will jump to the homepage
response.redirect(http://www.vevb.com)
End Sub
</SCRIPT>
Then debug any file in the current virtual directory, you will find that all pages jump to http://www.vevb.com/<
Through this example of forcing entry to a certain page, you can imagine that it is very necessary when the home page needs attention.
Let's continue to observe the Session_OnStart and Session_OnEnd events using an example of the number of people online.
2. 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 visits and the count is increased by 1
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 (PS if there is no such event program, the page access program will be 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
%>
There are currently <%=Application(OnLine)%> online
<a href=online.asp?logout=true>Exit</a>
You find that there is only one Application (OnLine) in this page, and it is still referenced explicitly. So where does its value come from? This is the key to the Global.asa file. You can open windows in sequence on this machine and perform debugging in two ways: close the window or exit.
3. Continue to refine
In the above program, you will find that the effect of closing the window after exiting the connection is different from that of closing the window directly. Because the Session exists for a long time, the Session_OnEnd event cannot be triggered when the window is closed directly, so how to realize this almost impossible idea?
As we all know, when a web page is closed, it can be associated with an onunload event. So as long as the onunload can perform the logout function of the Session, isn't that what we need? Without further ado, let’s 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)>
There are currently <%=Application(OnLine)%> online
<a href=online.asp?logout=true>Exit</a>
Note that when online.asp performs onunload, exit.asp will be opened. Then just set session.Abandon() in exit.asp and it will be OK.
4.exit.asp
<%session.Abandon()%>
<script>
self.close()
</script>
Of course, a Script is added to close the Session immediately after logging out. Now basically a web application for online statistics is enough.
4. In-depth study of Global.asa
From the above debugging, if you draw inferences from one example, you will definitely ask a question: How to control the number of registered users online? Then look at the following files one by one:
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
SubSession_OnStart
End Sub
SubSession_OnEnd
if session.contents(pass) then 'Determine whether it is the Session_OnEnd of the logged in user
application.lock
application(online)=application(online)-1
application.unlock
end if
End Sub
SubApplication_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, the OnStart event will be generated, and what is needed now is that the logged-in user is online, so Application (online) cannot be increased by 1 in the OnStart event.
And because the OnEnd event will be generated regardless of whether the session of the logged-in user ends (if a visitor visits the server but does not log in, the OnEnd event will also be generated after his session ends), so an if statement is used in the Session_OnEnd event to judge. Whether it is the OnEnd event of a logged-in user, if so, the number of people online will be reduced by 1.
And it is worth noting: the use of session.contents(pass), because the use of Session objects is prohibited in the OnEnd event, but the session variables can be called using a collection of Session objects. In other words, session(pass) cannot be written directly, but session.contents(pass) needs to be written.
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
%>
There are currently <%=application(online)%> registered members.
<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%>
It simply detects that when the user name is cnbruce and the password is cnrose, a session(pass)=true is generated, which is judged in Global.asa.
Five, continue to use your imagination
It is not enough to just count how many people are online, but also to determine the user's online status.
As you can imagine, the basic method is that when the user logs in, set online to 1 in login.asp (upload if there is a database), but when the user is offline, set online to 0. To improve it, you need to modify the Session_OnEnd event. Set online to 0 in this event (the value of 0 will also be uploaded)...