HOME

ASP Tutorial
ASP HOME
ASP Introduction
ASP Install
ASP Syntax
ASP Variables
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa

ASP Objects
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error

ASP FileSystem
ASP TextStream
ASP Drive
ASP File
ASP Folder
ASP Dictionary

ASP Components
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator

ASP Quick Ref

Examples/Quiz
ASP Examples
ASP Quiz Test

ADO
ADO Introduction

Resources
ASP Resources

Please visit our sponsors !

ASP Session Object

previous next

The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application.


The Session object

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.

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

The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires.


When does a Session Start?

A session can start when:

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

When does a Session End?

A session ends if a user has not requested or refreshed a page in the ASP application for a specified period. By default, this is 20 minutes.

If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property. The example below sets a timeout interval of 5 minutes.

<%
Session.Timeout=5
%>

To end a session you can also use the Abandon method.

<%
Session.Abandon
%>

Note: The main problem with sessions is WHEN they should end. We do not know if the user's last request was the final one or not. So we do not know how long to keep the sessions alive. Waiting too long uses up resources on the server. But if the session is deleted too fast you risk that the user is coming back and the server has deleted all the information, so the user has to start all over again. Finding the right timeout interval can be difficult.

Tip: If you are using session variables, store SMALL amounts of data in them.


Store and Retrieve Variable Values

The most important thing about the Session object is that you can store variables in it, like this:

<% 
Session("username")="Hege"
Session("age")=24
%>

The example above will set the Session variable username to Hege and the Session variable age to 24.

When the value is stored in a session variable it can be reached from any page in the ASP application by using: Session("username"):

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

The line above returns: "Welcome Hege".

You can also store user preferences in the Session object, and then access that preference to choose what page to return to the user. 

The example below specifies a text-only version of the page if the user has a low screen 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 Variable Values

The Contents collection contains all the variables that have been created and stored in a session. By using the Remove method, you can remove a variable from a session.

The example below removes an item, in this case a session variable named "sale":

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

You can also use the RemoveAll method to remove all variables in a session:

<%
Session.Contents.RemoveAll()
%>


Looping Through the Contents

You can loop through the Contents collection, to see what is stored in it:

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

Result:

username
age

If you do not 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
Hege
24


Looping Through the Objects

You can loop through the StaticObjects collection, to see the values of all the objects stored in the Session object:

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


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

IISProtect
Password Protect
Your Web Pages



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search