The session object has two events: the OnStart event and the OnEnd event. By scripting These two event procedures, you can perform the specified actions at the beginning and end of the session. When scripting code for these event procedures, you must use the script tag and set the RUNAT property to Server instead of using the generic ASP script delimiters <% and%>; the scripting code for these event procedures must be contained in a file named Global.asa, and The file must be stored in the root directory of the application.
1. Session_OnStart Event
The Session_OnStart event occurs when a new session is created by the server. The server processes the requested page before it is executed. The Session_OnStart event is the best time to set session variables, because they are executed before any page is accessed.
Triggers the Session_OnStart event when the session starts, and then runs the process of Session_OnStart events. That is, when the server receives an HTTP request for a URL in the application, it triggers the event and establishes a Session object.
2. Example
In the following example, session variables UserName and Password are initialized by Session_OnStart event procedures. The code is as follows:
<Script Language="VBScript" Runat="session">
Sub Session_OnStart
Session("UserName")="未知"
Session("Password")="未知"
End Sub
</Script>
If you do not want the user to go directly to a page outside the home page for the first time, you can compare the page path requested by the user to the home page path during the Session_OnStart process, and if not, call the Response.Redirect method to boot the user to the home page. The script is as follows:
<Script language="VBScript" runat="server">
sub session_OnStart
homePage="/myasp/index.asp"
requestpage=Request.ServerVariables("SCRIPT_NAME")
if homePage<>RequestPage then
response.Redirect homePage
end if
end sub
</script>
3. Test this Code
First we build a virtual directory with the alias "Myasp", save the Global.asa file to this virtual directory, and then build a simple home page called "index.asp" and put it under this virtual directory, then build a subdirectory named 1 under this virtual directory, then build a name of 1. ASP's simple page is placed in this subdirectory. Finally in the Address bar input address: http://127.0.0.1/myasp/1/1.asp, enter, you will find that the display is Index.asp page, the address bar automatically translated into 127.0.0.1/myasp/index.asp address. This indicates that when the customer first logs on to the Http://127.0.0.1/myasp/1/1.asp page, the Session_OnStart event is invoked first, in this event through the Request object's ServerVariables collection. SCRIPT_NAME the environment variable gets the virtual path to the current page and then compares it to the page path you specify, and if it's not the same, the page is redirected to the page you specify by the Redirect (redirection) method of the Response object.
4. Session_oneen Event
When the Session.Abandon method is invoked or the page is not requested or refreshed within the time-out period, the event occurs and the Sessiot_end event procedure is completed, and the corresponding session object automatically ends.
In the following example, an event procedure is used to add the time of the user identity and end session to the site log file (Session_OnEnd).
<script language="vbscript" runat="server">
sub session_OnEnd
response.AppendTolog Session.SessionID & Timer()
end sub
</script>
See the full set of ASP Getting started tutorials