ASP SessionObject


The Session object is used to store information about a user session, or to change settings for a user session.


Session Object

When you are working on an application on your computer, you open it, make changes, and then close it. This is much like a session. The computer knows who you are. It knows when you open and close the application. However, on the Internet, a problem arises: because HTTP addresses cannot maintain state, a Web server does not know who you are or what you do.

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the user's computer and contains information that identifies the user. This interface is called the Session object.

The Session object is used to store information about a user session, or to change settings for a user session.

Variables stored in the Session object store information about a single user and are available to all pages in an application. Common information stored in session variables is usually name, id, and parameters. The server creates a new Session for each new user, and destroys the Session object when the session expires.


When does a Session begin?

A Session begins when:

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

When does a Session end?

If a user does not request or refresh a page within the application for a specified period of time, the session ends. 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 theTimeoutproperty.

The following example sets a timeout interval of 5 minutes:

<%
Session.Timeout=5
%>

To end a session immediately, use theAbandonmethod:

<%
Session.Abandon
%>

Note:The main problem with using sessions is when they should end. We do not know if the user's last request is the final request. Therefore, we do not know how long to keep the session "alive". Waiting too long for an idle session can exhaust the server's resources. However, if the session is deleted too early, users have to start over and over again, because the server has already deleted all the information. Finding the appropriate timeout interval is difficult!

TipTip:Only store a small amount of data in session variables!


Store and Retrieve Session Variables

The biggest advantage of the Session object is that variables can be stored in it for later pages to read, and it has a wide range of applications.

The following example assigns "Donald Duck" to a Session variable namedusernameand assigns "50" to a Session variable namedage:

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

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

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

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

You can also store user preferences in the Session object, and then access these preferences to decide which page to return to the user.

The following example specifies that if the user uses a low screen 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 Variables

The Contents collection contains all session variables.

Session variables can be removed using 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
%>

To remove all variables in the session, use the RemoveAll method:

<%
Session.Contents.RemoveAll()
%>


Loop Through the Contents Collection

The Contents collection contains all session variables. You can view the variables stored in it by iterating through 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 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:

Session variables: 2
Donald Duck
50


Loop Through the StaticObjects Collection

You can view the values of all objects stored in the Session object by iterating through the StaticObjects collection:

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

Other extensions