Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of Global.asa files in ASP foundation

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes "what are the Global.asa file skills in the basics of ASP?" The content is detailed and easy to understand, "what are the Global.asa file skills in the ASP foundation?" interested friends can follow the editor's train of thought to read it slowly and deeply. I hope it can be helpful to everyone after reading. Let's follow the editor to learn more about "what are the Global.asa file skills in the foundation of ASP".

Global.asa files can manage two very demanding objects in ASP applications: Application and Session. As we all know, .asa is a file suffix. Is an acronym for Active Server Application.

It is actually an optional file in which the programmer can specify the event script and declare objects with session and application scope. 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. The file must be stored 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 they can be used as libraries for commonly used functions and subroutines. Global.asa files can only be used to create references and capture starts for objects, as well as to end Application and Session objects.

Global.asa files are accessed primarily based on session-level events and are called in the following three situations:

When the Application_OnStart or Application_OnEnd event is triggered.

When the Session_OnStart or Session_OnEnd event is triggered.

When referencing an object (Object) that is instantiated in the Global.asa file.

The standard file format for Global.asa is as follows:

Sub Application_OnStart 'Application_OnStart runs End Sub Sub Session_OnStart' Session_OnStart when any customer accesses the home page of the application, runs End Sub Sub Session_OnEnd 'Session_OnEnd when the customer * runs any page of the ASP application, runs End Sub Sub Application_OnEnd' Application_OnEnd when a customer's session times out or exits the application, runs End Sub when the site's WEB server is shut down

1. Session_onStart

First, take a look at a code that controls the user's access to the page:

1Global.asa (placed under the root of the virtual directory being debugged)

Sub Session_OnStart'as long as the user logs in to this site for * times, he or she will jump to the home page response.redirect ("http://www.webjx.com/") End Sub")

Then debug any files in the current virtual directory and you will find that all the pages jump to http://www.webjx.com/<

Through this example of "forcing access to a page", you can imagine that it is necessary when the home page needs to be paid attention to.

Let's take an example of "number of people online" to continue to observe Session_OnStart and Session_OnEnd events.

II. Session_onEnd

2Global.asa (placed under the root of the virtual directory being debugged)

The initial value of Sub Application_onStart'is 0 Application ("OnLine") = 0 End Sub Sub Session_onStart 'for a user visit plus 1 Application.Lock Application ("OnLine") = Application ("OnLine") + 1 Application.Unlock End Sub Sub Session_OnEnd' the end of a user process, minus 1 (P.S. If there is no such event program, the page access program is executed. ) Application.Lock Application ("OnLine") = Application ("OnLine")-1 Application.Unlock End Sub

3,online.asp

Currently, there is a total online exit.

You find that there is only one Application ("OnLine") on the page, and it is referenced by the display. So where does it come from? This is the key to Global.asa files. You can open a window in turn on this machine and debug it either by closing the window or by exiting.

Third, continue to refine.

In the above program, you will find that the effect of closing the window after "exiting" the connection is not the same as closing the window directly. Because Session exists for a long time, when you close the window directly, you can't trigger the Session_OnEnd event, so how do you achieve this almost impossible idea?

As we all know, when the page is closed, it can be accompanied by an onunload event, so as long as the onunload can perform the logout function of Session, isn't that what we need? Cut the crap and modify the online.asp

Currently, there is a total online exit.

Notice that online.asp opens exit.asp when it does onunload, so just set session.Abandon () in exit.asp to OK.

Exit.asp

Self.close ()

Of course, a Script script has been added to close itself immediately after logging out of Session. Now basically a Web application for online statistics will be fine.

Fourth, in-depth study of Global.asa

From the above debugging, you will certainly ask a question: how to control the number of registered users online? Then look at the following documents one by one:

5Global.asa (placed under the root of the virtual directory being debugged)

Sub Application_OnStart application ("online") = 0 End Sub Sub Session_OnStart End Sub Sub Session_OnEnd if session.contents ("pass") then'to determine whether it is the Session_OnEnd application.lock application ("online") = application ("online")-1 application.unlock end if End Sub Sub Application_OnEnd End Sub of the logged-in user

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 or not, an OnStart event is generated, and now all you need is the login user's online, so you cannot add 1 to the Application ("online") in the OnStart event.

And because the OnEnd event will occur at the end of the session of the logged-in user or not (if a visitor visits the server but does not log in, the OnEnd event will also occur after the end of his session), so an if statement is used in the Session_OnEnd event to determine whether it is the OnEnd event of the logged-in user, and if so, the number of people online will be reduced by 1.

And it's worth noting the use of session.contents ("pass") because the use of Session objects is prohibited in OnEnd events, but the session variable can be called with a collection of Session objects. In other words, you can't write session ("pass") directly, but you need to write session.contents ("pass").

6,login.asp

Current registered member.

Quit

Simply check that when the name is cnbruce and the password is cnrose, a session ("pass") = true is generated, which is judged in the Global.asa.

Fifth, continue to use your imagination.

Think, think again. It is not enough to count how many people are online, but also to judge the online status of users.

You can imagine that basically, when the user logs in, set online to 1 in login.asp (upload if there is a database), but set online to 0 when the user is offline. To improve it, it is necessary to modify the Session_OnEnd event, in which online is set to 0 (also upload a value of 0).

Of course, Global.asa is much more than that. However, we are not in a hurry to master all of them now. When we come into contact with the database, we will come back and continue to study the file. I believe we will learn a lot by then. So, let's eat through the above first.

On the basis of ASP Global.asa file skills on which to share here, I hope that the above content can improve you. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report