The global.asa file is an optional file. Users can declare session and application-wide objects in the specified event script. Today, the editor of the Fooxin Technology Channel has specially collected and sorted out relevant information. Interested friends can go to the following article to learn more!
We all know that .asa is the file suffix name. It is the acronym for Active Server Application.
It is actually an optional file in which the program writer can specify event scripts and declare objects with session and application scopes. The contents of this file are not used to display to the user, but to store event information and objects used globally by the application. This file must be stored in the root directory of the application. Each application can only have one Global.asa file.
The most common misconception about Global.asa files is that it can be used as a library for general use functions and subroutines. The Global.asa file can only be used to create references and capture startups of objects, as well as end Application objects and Session objects.
The Global.asa file is accessed based on session-level events and is called in the following three cases:
The standard file format of Global.asa is as follows:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Application_OnStart 'Application_OnStart Run End Sub Sub Session_OnStart The first time a customer accesses the homepage of the application 'Session_OnStart Run End Sub Sub Session_OnEnd 'Session_OnEnd Run End Sub Session_OnEnd 'Session_OnEnd Run End Sub Application_OnEnd 'Application_OnEnd Run End Sub when the site's WEB server is down </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 debugged virtual directory)
<SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub Session_OnStart 'As long as the user logs into this site for the first time, he will jump to the homepage response.redirect(http://www.jb51.net) End Sub </SCRIPT>
Then debug any files in the current virtual directory, and you will find that all pages jump to http://www.webjx.com/<
Through this example of "forced entry into a certain page", it can be imagined that it is very necessary when the home page needs to be followed.
Let’s continue to observe the Session_OnStart and Session_OnEnd events with an example of “number of online users”
2. Session_onEnd
2. Global.asa (placed under the root directory of the debugged virtual directory)
<SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Application_onStart 'Initial value is 0 Application("OnLine") = 0 End Sub Sub Session_onStart 'A user access adds 1 Application.Lock Application("OnLine") = Application("OnLine") + 1 Application.Unlock End Sub Sub Session_OnEnd 'The end of a user process, count down by 1 (PS If there is no event program, 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 %> There are currently <%=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 referenced. So where does its value come from? This is the key to the Global.asa file. You can open windows in this machine, close windows or exit debugging of the two methods.
3. Continue to refine it
You will find that the effects of closing the window after "exit" connection are different from closing the window directly. Because Session exists in time, when closing the window directly, the Session_OnEnd event cannot be triggered, so how can this almost impossible idea be realized?
As we all know, when a web page is closed, it can be accompanied by an onunload event. So as long as the onunload can be executed, isn't it all we need? Say less nonsense, modify 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 is onunload, exit.asp will be opened. Then just set session.Abandon() in exit.asp and not OK.
exit.asp
<%session.Abandon()%> <script> self.close() </script>
Of course, a Script script was added to close itself immediately after the Session was logged out. Now, just a web application with online statistics is enough.
4. In-depth study of Global.asa
From the above debugging, you will learn from one example and apply it to others and will definitely ask a question: How to control the number of registered users online? Let’s read the following documents one by one:
4. 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 logged-in 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 cause any events.
Because once a user accesses the server, regardless of whether the user logs in or not, an OnStart event will be generated. Now all you need is to log in to the user's online, so you cannot add 1 to the OnStart event.
Also, because the OnEnd event will be generated regardless of whether the session of the logged-in user is over (if a visitor visits the server but is not logged in, the OnEnd event will also be generated after the session ends), so a if statement is used in the Session_OnEnd event to determine whether it is the OnEnd event of the logged-in user. If so, the number of people online will be reduced by 1.
And it is worth noting that the use of session.contents("pass") is because the use of Session objects is prohibited in the OnEnd event, but the session variable can be called using a collection of Session objects. In other words, you cannot write session("pass") directly, but you need to write session.contents("pass").
5. login.asp
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%>
Just simply detect that when the name of cnbruce and the password is cnrose, a session("pass")=true is generated, which is judged in Global.asa.
5. Continue to use your imagination
Think about it, think about it again. It is not enough to just count how many people are online, and it also requires judging the user's online status.
You can imagine the basic method. When the user logs in, set the online to 1 in login.asp (upload if there is a database), but when the user is offline, set the online to 0. To improve it, you need to modify the Session_OnEnd event and set the online to 0 in the event (the same value will be uploaded)...
Of course, Global.asa is much more than that. But we don’t have to rush to master it all now. When we come into contact with the database, we will return to look at it and continue to study the file. I believe that by then we will definitely realize a lot. So, let’s understand the above thoroughly first.
I hope that through this article, I will briefly analyze the usage of Global.asa files of ASP, which can bring you more help. More technical knowledge will be available on the Wuxin Technology Channel Network!