ASP  «Prev  Next»

Lesson 3ASP's Session object
Objective Describe how the Session object maintains user information.

ASP's Session Object

The Session object in ASP allows data related to a specific user to be stored in memory on the server for the duration of that user's session. In practical terms, this means that any Web page on the Web site can read, update, and store user information, making it available to any other Web page on the site as the user navigates from page to page setting preferences and making selections.
An individual user's Session object, an array that can grow as more data is stored for that user, is created by ASP when a user first contacts the website. The user is associated with a particular set of session data through an ASP-generated Session ID, sent as a cookie to the user's browser.
Unlike the browser cookies discussed in a previous module, this session cookie is a temporary one: it does not have an expiration date and time, and it does not store any data fields, just the Session ID. When the user closes his or her Web browser, session cookies are not saved.

Establishing a Session ID

The following Slideshow illustrates the process of creating a Session ID
1) Session Object 1 2) Session Object 2 3) Session Object 3 4) Session Object 4 Program 1 Program 2 Program 2 Program 2
Establishing sessionId

User's Session ID

An ASP Session object and unique Session ID are created for each user. However, be aware that Session IDs are generated uniquely only as long as the Web server computer and software are running. If the computer is restarted, or if the Web service program (Personal Web Server or its equivalents) is stopped and started again, there may be duplication of Session IDs with ones generated earlier.
It is possible to assign a Session ID to a user as a permanent User ID for your site. Because there is the chance that Session IDs may be duplicated upon a server reboot, you should not employ a Session ID as a permanent User Identification.

Using Session variables

Although the Session object has some useful attributes (such as TimeOut and SessionID), the real power of the Session object is in storing values throughout a session. Using Session variables is a powerful solution because you can store objects as well as simple strings or numbers. For example, you can use Session variables to store:
  1. Database connections - Create a connection to a database (which can be a resource drain on a server) just once for a user, and use the same connection over and over again through a Session variable.
  2. Items in an online shopping cart - A two-dimensional array holding multiple items (each with Product ID, quantity, and price) can be set up as a Session variable.
  3. A background image - Each user could select from among several background images, and a Session variable could store the filename of the image to incorporate into the Web pages' HTML code.

The next lesson describes the Global.asa file.