Recommended: Interpretation of how to correctly use Session object variables in ASP Anyone who uses ASP knows that a Session object stores the information needed for a specific user session, and when a user jumps between pages of the application, variables stored in the Session object are not cleared, and these variables are always present when the user accesses the page in the application. It can reduce the complexity of the program and improve programming efficiency, but it also has many shortcomings.
1. Attributes1. SessionID
The SessionID property returns the user's session identity. When creating a session, the server generates a separate identity for each session. The session identifier is returned as the elongated data type. In many cases, SessionID can be used for WEB page registration statistics.
2. TimeOut
The Timeout property specifies a timeout time limit for the Session object of the application in minutes. If the user does not refresh or request a web page within this timeout period, the session will terminate.
2. Method
There is only one method for Session objects, which is Abandon. The Abandon method deletes all objects stored in Session objects and releases the source of these objects. If you do not explicitly call the Abandon method, the server will delete these objects once the session timed out. When the server has finished processing the current page, the following example releases the session state.
<%Session.Abandon%>
III. Events
The Session object has two events that can be used when the Session object is started and released is run.
1. The Session_OnStart event occurs when the server creates a new session. The server processes the script before executing the requested page. The Session_OnStart event is the best time to set session variables, because they are set before accessing any pages.
Although the Session object remains when the Session_OnStart event contains Redirect or End method calls, the server will stop processing the Global.asa file and trigger the script in the file that triggers the Session_OnStart event.
To ensure that the user always starts a session when opening a specific web page, the Redirect method can be called in the Session_OnStart event. When the user enters the application, the server creates a session for the user and processes the Session_OnStart event script. You can include the script in this event to check if the page opened by the user is a startup page, and if not, instruct the user to call the Response.Redirect method to start the web page. The procedure is as follows:
<SCRIPTRUNAT=ServerLanguage=VBScript>
SubSession_OnStart
startPage=/MyApp/StartHere.asp
currentPage=Request.ServerVariables(SCRIPT_NAME)
ifstrcomp(currentPage,startPage,1)then
Response.Redirect(startPage)
endif
EndSub
</SCRIPT>
The above programs can only run in browsers that support cookies. Because browsers that do not support cookies cannot return SessionIDcookies, the server creates a new session whenever a user requests a web page. This way, for each requesting server, the Session_OnStart script will be processed and the user will be redirected to the startup page.
2. The Session_OnEnd event occurs when the session is abandoned or timed out.
Regarding the matters that need to be paid attention to when using Session objects, please refer to the previous article.
The session can be started in the following three ways:
1. A new user requests access to a URL that identifies the .asp file in an application, and the Global.asa file of the application contains the Session_OnStart process.
2. The user stores a value in the Session object.
3. The user requested an application's .asp file, and the application's Global.asa file uses the <OBJECT> tag to create an instance of an object with a session scope.
If the user does not request or refresh any pages in the application within the specified time, the session will automatically end. The default value for this period is 20 minutes. The default timeout limit settings for an application can be changed by setting the session timeout property in the Application Options property page in the Internet Service Manager. This value should be set according to the requirements of your web application and the memory space of the server. For example, if you want users browsing your web application to stay on each page for only a few minutes, you should shorten the default timeout value for your session. An overly long session timeout value will cause too many open sessions and exhaust your server's memory resources. For a specific session, if you want to set a timeout value that is smaller than the default timeout value, you can set the Timeout property of the Session object. For example, the following script sets the timeout value to 5 minutes.
<%Session.Timeout=5%>
Of course, you can also set a timeout value greater than the default setting. The Session.Timeout property determines the timeout value. You can also explicitly end a session through the Abandon method of the Session object. For example, provide an exit button in the table, setting the ACTION parameter of the button to the URL of the .asp file containing the following commands.
<%Session.Abandon%>
Share: How ASP prevents repeated submissions of forms Users often encounter some unexpected situations when submitting forms, such as refreshing the page multiple times, pressing the back key, etc. If control measures are not taken, it will cause the problem of repeated submission of the form. The prevention method introduced in this article is mainly composed of four subroutines. In relatively simple applications, you just need to place these codes in the included file and directly reference them; for those