ASP Session Object

The Session object is used to store user information. The variables stored in the session object hold information for a single user and are available on all pages of an application.

Session object

When you operate an application, you open it, make some changes, and then close it. This is very similar to a conversation (Session). The computer knows who you are. It is clear when you open and close the application. But there is a problem on the Internet: since HTTP addresses cannot retain state, web servers do not know who you are or what you have done.

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the client and contains information that can identify the user. This interface is called the Session object.

The Session object is used to store information about the user, or to change the settings for a user's session. The variables stored in the session object contain information about a single user and are available on all pages in the application. The information stored in the session object is usually name, id, and parameters. The server creates a new Session for each new user and revokes the Session object when the session expires.

When does the session start?

Session begins:

  • When a new user requests an ASP file and the Global.asa file references the Session_OnStart subroutine;
  • When a value is stored in the Session variable;
  • When a user requests an ASP file and the Global.asa file uses the <object> tag to instantiate an object through the session scope;

When does the session end?

If the user does not request or refresh the page within the specified time in the application, the session will end. The default value is 20 minutes.

If you want to set a longer or shorter timeout interval, you can set Timeout Property.

The following example sets a timeout interval of 5 minutes:

<%
Session.Timeout=5
%>

To end the session immediately, you can use Abandon Method:

<%
Session.Abandon
%>

Note:The main problem when using session is determining when they should end. We do not know whether the user's recent request is the final request. Therefore, we are not sure how long the session should "survive". Waiting too long for an idle session will exhaust the server's resources. However, if the session is deleted too early, the user will have to start over again and again, because the server has deleted all the information. Finding the appropriate timeout interval is very difficult.

Tip:If you are using session variables, do not store a large amount of data in them.

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 scope is very wide.

The following example assigns the value "Donald Duck" to the session variable named username, and assigns the value "50" to the session variable named age:

<%
Session("username")="Donald Duck"
Session("age")=50
%>

Once a value is stored in the session variable, it can be used by any page in the ASP application:

Welcome <%Response.Write(Session("username"))%>

The result returned by the above line of code is: "Welcome Donald Duck".

You can also save 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 display resolution, a plain text version of the page is returned:

<%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 session variables.

You can remove session variables by using the remove method.

In the following example, if the value of the session variable "age" is less than 18, remove the session variable "sale":

<%
If Session.Contents("age")<18 then 
  Session.Contents.Remove("sale")
End If 
%>

If you need to remove all variables from the session, please use the RemoveAll method:

<%
Session.Contents.RemoveAll()
%>

Traverse the contents collection

The contents collection contains all session variables. You can view the stored variables by traversing the contents collection:

<%
Session("username")="Donald Duck"
Session("age")=50
dim i
For Each i in Session.Contents
  Response.Write(i & "<br />")
Next
%>

Result:

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
%>

Result:

Session variables: 2
Donald Duck
50

Traverse the StaticObjects collection

You can iterate through the StaticObjects collection to view the values of all objects stored in the session object:

<%
dim i
For Each i in Session.StaticObjects
  Response.Write(i & "<br />")
Next
%>