This article mainly introduces the use of Global.asa file. It is an optional file. Each application can only have one Global.asa file. Friends who need to know can refer to it.
Next, I will introduce to you how to use Global.asa file.
What is the Global.asa file? 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. The name of the file must be Global.asa and must be stored in the root directory of the application. Each application can only have one Global.asa file.
In the Global.asa file, if the included script is not encapsulated with <SCRIPT> tag, or the defined object has no session or application scope, the server returns an error. We can write scripts included in the Global.asa file in any script-enabled language. If multiple events are in the same scripting language, they can be organized in a set of <SCRIPT> tags.
The process declared in the Global.asa file can only be called from one or more scripts related to the Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. They are not available in the ASP page of ASP-based applications. If you want to share procedures between applications, you can declare them in a separate file and then use the Server-Side Inclusion (SSI) statement to include the file in the ASP program that calls the procedure. Generally, the extension of the containing file should be .inc.
Global.asa file:
Here is a very standard
- <SCRIPTLANGUAGE=VBScriptRUNAT=Server>
- 'Session_OnStart Runs when the client first runs any page in the ASP application
- 'Session_OnEnd runs when a client's session timed out or exits the application
- 'Application_OnStart runs when any customer first accesses the homepage of the application
- 'Application_OnEnd runs when the site's WEB server is down
- </SCRIPT>
- <SCRIPTLANGUAGE=VBScriptRUNAT=Server>
- SubApplication_OnStart
- VisitorCountFilename=Server.MapPath(/ex2)+/VisitCount.txt
- SetFileObject=Server.CreateObject(Scripting.FileSystemObject)
- SetOut=FileObject.OpenTextFile(VisitorCountFilename,1,FALSE,FALSE)
- Application(visitors)=Out.ReadLine
- Application(VisitorCountFilename)=VisitorCountFilename
- EndSub
- '=================================================================== =========
- SUBApplication_OnEnd
- SetFileOutObject=Server.CreateObject(Scripting.FileSystemObject)
- SetOut=FileOutObject.CreateTextFile(Application(VisitorCountFilename),TRUE,FALSE)
- Out.WriteLine(application(visitors))
- EndSub
- '=================================================================== ==========SubSession_OnStart
- Session.Timeout=5
- Application(visitors)=Application(visitors)+1
- Session(ID)=Session.SessionID
- EndSub
- </SCRIPT>
In this Global.asa program, the File Access component of ASP is involved, which provides methods, properties, and collections for accessing the file system. This will be discussed in future ASP components. Here, it serves to create new files on the server and write to the files. This is actually a Global file of an ASP page access counter application. First, when the customer first accesses the homepage of the application, the process Application_OnStart defines a text file of VisitCount.txt in the virtual directory specified on the server, and Save the path and content of the file in application-level variables. When any client accesses any page in an ASP application, the process Session_OnStart definition automatically adds the value of the application-level variable visitors. In this way, whenever a customer visits the page, the variable visitors will be automatically added to the function of counting click-through rate. Since the value of the variable visitors is stored in the system memory, if the server is shut down or restarted, the data stored in the variable will be automatically lost. Therefore, by defining the process Application_OnEnd, the data is written to the established before the server is shut down or restarted. In the text file, this ensures that when the server starts again, the Application_OnStart process can read previous statistics from the VisitCount.txt file.
The above is all about this article, I hope it will be helpful to everyone's learning.