Previous PageNext Page

6.5 Session Object

The Session object stores information needed for a particular user-session.
 
Variables stored in the Session object are not discarded when the user jumps between pages in the application; instead, they persist for the entire user-session. The Web server automatically creates a Session object when a Web page (from a server application) is requested by a user who does not already have a session. The server destroys the Session object when the session expires or is abandoned.
 
The AllowSessionState registry or configuration file setting controls the creation of Session objects. If AllowSessionState is set to False, the Session object cannot be used. The default is to use Sessions.
 
Session object event scripts are declared in the Global.asa.
 

Note:

Session state is only maintained for browsers that support cookies.

Syntax

Session.collection|property|method

Collections

Contents
 
Contains the items you have added to the session with script commands.
 
StaticObjects
 
Contains items created in Global.asa using the <OBJECT> tag and given session scope.
 

Properties

SessionID
 
Returns the session identifier for this client. Each session has a unique identifier.
 
Timeout
 
The timeout period, in minutes, for session state for this application.
 
The CodePage and LCID properties are not currently implemented in Chili!Soft ASP.
 

Methods

Abandon
 
Destroys a Session object and all object stored in it and releases their resources.
 

Events

Session_OnStart
 
Occurs when the server creates a new session. It runs before executing the requested page.
 
Session_OnEnd
 
Occurs when the session is abandoned or times out.
 
Session object events are defined in the Global.asa file.
 

Remarks

You can store values in the Session object. Information stored in the Session object is available for the entire session and has session scope. The following script demonstrates how two types of variables are stored:
 

Session("username") = "Janine"

Session("age") = 42

If you are using VBScript as your scripting language, you must use the Set keyword to store an object in the Session object, as shown in the following example:
 

<% Set Session("Obj1") = Server.CreateObject("MyComponent") %>

You can then call the methods and properties of Obj1 on subsequent Web pages by using the following syntax:
 

<% Session("Obj1").MyObjMethod %>

As an alternative, you can extract a local copy of the object:
 

Set MyLocalObj = Session("Obj1")

MyLocalObj.MyObjMethod

You cannot store a built-in object in a Session object. Each of the following lines will return an error:
 

Set Session("var1") = Session

Set Session("var2") = Request

Set Session("var3") = Response

Set Session("var4") = Server

Set Session("var5") = Application

Before you store an object in the Session object, you must know what threading model it uses. Only objects marked as both free and apartment-threaded can be stored in the Session object.
 
The Session object is implemented as a collection. If you store an array in an Session object, you should not attempt to alter elements of the stored array directly. For example, the following script does not work:
 

Session("StoredArray") (3) = "new value"

Instead of storing the value "new value" in StoredArray(3), the value is stored in the Session collection, overwriting any information stored at Session(3).
 
See the Application object for example of storing an array.
 

Copyright © 2000 Chili!Soft

Previous PageTop Of PageNext Page