Session Object
You can use the Session object to store 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, these variables persist for the entire user-session.
The Web server automatically creates a Session object when a Web page from the 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.
Note Session state is only maintained for browsers that support cookies.
Syntax
Session.collection|property|method .
Session Collections
Session Contents Collection
The Session.Contents collection contains all of the items that have been established for a session without using the <OBJECT> tag. The collection can be used to determine the value of a specific session item, or to iterate through the collection and retrieve a list of all items in the session.
Syntax
Session.Contents( Key )
Parameters
Key
The name of the property to retrieve.
Remarks
You can use an iterating control structure to loop through the keys of the Contents collection. This is demonstrated in the following example.
<%
Dim sessitem
For Each sessitem in Session.Contents
Response.write(sessitem & " : " & Session.Contents(sessitem) & "<BR>")
Next
%>
Session StaticObjects Collection
The StaticObjects collection contains all of the objects created with the <OBJECT> tag within the scope of the session object. The collection can be used to determine a the value of a specific property for an object, or to iterate through the collection and retrieve all properties for all objects.
Syntax
Session.StaticObjects( Key )
Parameters
Key
The property to retrieve.
Remarks
You can use an iterating control structure to loop through the keys of the StaticObjects collection. This is demonstrated in the following example.
<%
Dim objprop
For Each objprop in Session.StaticObjects
Response.write(objproperty & " : " & Session.StaticObjects(objprop) & "<BR>")
Next
%>
Session Properties
CodePage
The CodePage property determines the codepage that will be used to display dynamic content.
Syntax
Session.CodePage(=Codepage)
Parameters
Codepage
An unsigned integer that represents a valid codepage for the system that is running the ASP scripting engine.
Remarks
A codepage is a character set that can include numbers, punctuation marks, and other glyphs. Different languages and locales may use different codepages. For example, ANSI code page 1252 is used for American English and most European languages; OEM code page 932 is used for Japanese Kanji.
A codepage can be represented in a table as a mapping of characters to single-byte values or multibyte values. Many codepages share the ASCII character set for characters in the range 0x00 – 0x7F.
LCID
The LCID property determines the location identifier that will be used to display dynamic content.
Syntax
Session.LCID(=LCID)
Parameters
LCID
A valid locale identifier.
Remarks
An LCID specifies the locale identifier which is a standard international abbreviation that uniquely identifies one of the system-defined locales.
SessionID
The SessionID property returns the session identification for this user. Each session has a unique identifier that is generated by the server when the session is created. The session ID is returned as a LONG data type.
Syntax
Session.SessionID
Remarks
You should not use the SessionID property to generate primary key values for a database application. This is because if the Web server is restarted, some SessionID values may be the same as those generated before the server was stopped. Instead, you should use an autoincrement column data type, such as IDENTITY with Microsoft® SQL Server, or COUNTER with Microsoft® Access.
Timeout
The Timeout property specifies the timeout period assigned to the Session object for this application, in minutes. If the user does not refresh or request a page within the timeout period, the session ends.
Syntax
Session.Timeout [ = nMinutes]
Parameters
nMinutes
Specifies the number of minutes that a session can remain idle before the server terminates it automatically. The default is 20 minutes.
Session Methods
Abandon
The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.
Syntax
Session.Abandon
Remarks
When the Abandon method is called, the current Session object is queued for deletion, but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to Abandon, but not in any subsequent Web pages.
For example, in the following script, the third line prints the value Mary. This is because the Session object is not destroyed until the server has finished processing the script.
<%
Session.Abandon
Session("MyName") = "Mary"
Reponse.Write(Session("MyName"))
%>
If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with previous Session object when the page containing the above example finished processing.
The server creates a new Session object when you open a subsequent Web page after abandoning a session. You can store variables and objects in this new Session object.
Examples
The following example causes the session state to be released when the server finishes processing the current page.
<% Session.Abandon %>
