ASP Session Object
The Previous page Next page Session object is used to store the user's information. stored in the Session object.
Variables hold the information of a single user and are available for all pages in an application.
Session Object
When you manipulate an application, you open it, make some changes, and then close it. It's much like a conversation (session).
The computer knows who you are. It knows when you open and close the application. But there is a problem on the Internet:
Because the HTTP address cannot be persisted, the Web server does not know who you are and what you do.
ASP solves this problem by creating a unique cookie for each user. The cookie is transmitted to the guest
The user, which contains information that can identify users. This interface is called the Session object.
Session objects are used to store information about a user, or to change settings for a user's session. stored in
The variables in the session object hold a single user's information and are available to all pages in the application.
The information stored in the Session object is typically name, ID, and parameters. Each new user is created by the server
A new session, and the session object is dropped when the session expires.
When does the Session start?
Session begins at:
When a new user requests an ASP file, and the Global.asa file references the Session_OnStart child
procedures;
When a value is stored in the Session variable;
When a user requests an ASP file and Global.asa uses the <object> tag to pass the session
Scope to sample an object;
When does the session end?
If the user does not request or refresh the page in the application within the specified time, the session will end. Default
The value is 20 minutes.
If you want to set the time-out interval to be longer or shorter, you can set the Timeout property.
The following example sets the time-out interval of 5 minutes:
<%
Session.timeout=5
%> to end the session immediately, you can use the Abandon method:
<%
Session.Abandon
%> Note: The main question when using the session is when they should end. We will not know the user's most recent request
is the last request. So we don't know how long it will take for the session to "survive". For an idle session.
Waiting too long will drain the server's resources. However, if the session is deleted prematurely, the user will have to do it again.
Start over again, because the server has deleted all the information. Finding the appropriate time-out interval is
Very difficult.
Tip: If you are using session variables, do not store large amounts of data in them.
Storing and retrieving session variables
The biggest advantage of the Session object is that it can store variables for subsequent web pages to be read in a wide range of applications.
Of
The following example assigns "Donald Duck" to a session variable named username and assigns "50" to the value
To a session variable named Age: Taiyuan 264 Hospital
<%
Session ("username") = "Donald Duck"
Session ("Age") =50
%> once the value is stored in the session variable, it can be used by any page in the ASP application:
Welcome <%response.write ("username")%> The result returned by this line of the program is:
"Welcome Donald Duck".
You can also save user parameters in the Session object and then access those parameters to determine what page to return to the user
Surface.
The following example specifies that a plain text version of a page is returned if the user is using a low display resolution:
<%if Session ("screenres") = "Low" then%>
This is the text version of the page
<%Else%>
This is the multimedia version of the page
<%end if%> Remove Session variable
The Contents collection contains all the session variables.
The Remove method can be used to remove the session variable.
In the following example, if the value of the session variable "age" is less than 18, then the session variable "sale" is removed:
<%
If session.contents ("age") <18 Then
Session.Contents.Remove ("Sale")
End If
%> to remove all variables in the session, use the RemoveAll method:
<%
Session.Contents.RemoveAll ()
%> Traversal Contents Collection
The Contents collection contains all the session variables. The contents collection can be traversed to see where the storage
The variables:
<%
Session ("username") = "Donald Duck"
Session ("Age") =50
Dim i
For each i in session.contents
Response.Write (I & "<br/>")
Next
%> Result: Male in Taiyuan 264 hospital
Username
Age if you need to know the number of items in the Contents collection, you can use the Count property:
<%
Dim i
Dim j
J=session.contents.count
Response.Write ("Session variables:" & j)
For I=1 to J
Response.Write (Session.Contents (i) & "<br/>")
Next
%> results:
Session Variables:2
Donald Duck
50 Times Calendar StaticObjects Collection
You can view the values of all objects stored in the session object by looping through the StaticObjects collection:
<%
Dim i
For each i in session.staticobjects
Response.Write (I & "<br/>")
Next
%>