ASP Session
The Session object is used to store information about the user session (session), or to change the settings of the user session (session).
Session Object
When you work on an application on your computer, you open it, make changes, and then close it. It's a lot like a conversation. The computer knows who you are. It knows when you open and close apps. However, on the Internet a problem arises: since HTTP addresses cannot maintain state, the web server has no idea who you are and what you do.
ASP solves this problem by creating a unique cookie for each user. A cookie is transferred to the user's computer and contains information that identifies the user. This interface is called a Session object.
The Session object is used to store information about the user session (session), or to change the settings of the user session (session).
Variables stored in the Session object store information about a single user and are available to all pages in the application. The common information stored in session variables is usually name, id, and parameters. The server will create a new Session for each new user and revoke the Session object when the session expires.
When does the Session start?
Session starts when:
A new user requests an ASP file, and the Global.asa file references the Session_OnStart subroutine
A value is stored in the Session variable
A user requested an ASP file, and Global.asa was instantiated through the session scope using the <object> tag When does an object
#Session end?
If the user does not request or refresh the page in the application within the specified time, the session will end. The default value is 20 minutes.
If you want to set the timeout interval to be shorter or longer than the default value, you can use the Timeout property.
The following example sets a 5-minute timeout interval:
Session.Timeout=5
%>
To end the session immediately, please use the Abandon method:
Session.Abandon
%>
Note: The main issue when using sessions is when they should end. We won't know if the user's most recent request was the last request. So we don't know how long we should let the session "live". Waiting too long for an idle session can exhaust server resources. However, if the session is deleted prematurely, the user will have to start over again over and over again because the server has deleted all the information. Finding the right timeout interval can be difficult!
Tips: Only a small amount of data is stored in the session variable!
Storing and retrieving Session variables
The biggest advantage of the Session object is that variables can be stored in it for subsequent web pages to read, and its application range is very wide.
The following example assigns "Donald Duck" to the Session variable named username, and assigns "50" to the Session variable named age:
Session("username")="Donald Duck"
Session("age")=50
%>
When the value is stored in the session variable, it can be used by any page in the ASP application:
The result returned by the above line of code is: "Welcome Donald Duck".
You can also store user parameters in the Session object and then access these parameters to decide what page to return to the user.
The following example specifies that if the user uses a low monitor resolution, a text-only version of the page is returned:
This is the text version of the page
<%Else%>
This is the multimedia version of the page
<%End If%>
Remove Session variables
The Contents collection contains all session variables.
The session variable can be removed through the Remove method.
In the following example, if the value of the session variable "age" is less than 18, the session variable "sale" is removed:
If Session. Contents("age")<18 then
Session.Contents.Remove("sale")
End If
%>
If you need to remove the For all variables, use the RemoveAll method:
Session.Contents.RemoveAll()
%>
Traverse the Contents collection
The Contents collection contains all session variables. You can view the variables stored in the Contents collection by looping through it:
Session("username")="Donald Duck"
Session("age")= 50
dim i
For Each i in Session.Contents
Response.Write(i & "<br>")
Next
%>
Result:
age
If you don't 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
%>
Result:
Donald Duck
50
Traverse the StaticObjects collection
You can view the information stored in the Session by traversing the StaticObjects collection The value of all objects in the object:
dim i
For Each i in Session.StaticObjects
Response.Write(i & "<br>" )
Next
%>