please dont rip this site

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.

One common use for the Session object is to store user preferences set on a previous visit to the Web application, such as high, medium, or low graphics.

Note Session state is only maintained for browsers that support cookies. Session variables are specific to the ASP pages "application" (read: directory that the ASP page is stored in). The session ID will be the same, but the variables will not be accessable unless each script is running from the same directory.




Returns the session identification for this user.


The timeout period for the session state for this application, in minutes.



This method destroys a Session object and releases its resources.


You can store values in the Session object. Information stored in the Session object is available throughout the session and has session scope. The following script demonstrates storage of two types of variables.

   Session("username") = "Janine"
   Session("age") = 24

However, if you store an object in the Session object and use Visual Basic® Scripting Edition as your primary scripting language you must use the Set keyword. This is illustrated in the following script.

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

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

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

Or by extracting a local copy of the object and using the following:

Set MyLocalObj1 = Session("Obj1") 

Another way to create objects with session scope is by using the <OBJECT> tag in the Global.asa file. For more information, see the Global.asa Reference.

You cannot, however, store a built-in object in a Session object. For example, each of the following lines would 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 should know what threading model it uses. Only objects marked as both free- and apartment-threaded can be stored in the Session object without locking the session to a single thread. For more information, see Threading Models in Creating Components for ASP.

If you store an array in a Session object, you should not attempt to alter the elements of the stored array directly. For example, the following script will not work:

<% Session("StoredArray")(3) = "new value" %>

This is because the Session object is implemented as a collection. The array element StoredArray(3) does not receive the new value. Instead, the value is indexed into the collection, overwriting any information stored at that location.

It is strongly recommended that if you store an array in the Session object, you retrieve a copy of the array before retrieving or changing any of the elements of the array. When you are done with the array, you should store the array in the Session object all over again, so that any changes you made are saved. This is demonstrated in the following example:

'Creating and initializing the array
dim MyArray()
Redim MyArray(5)
MyArray(0) = "hello"
MyArray(1) = "some other string"

'Storing the array in the Session object
Session("StoredArray") = MyArray


'Retrieving the array from the Session Object
'and modifying its second element
LocalArray = Session("StoredArray")
LocalArray(1) = " there"

'printing out the string "hello there"

'Re-storing the array in the Session object
'This overwrites the values in StoredArray with the new values
Session("StoredArray") = LocalArray


Session("name") = "MyName" 
Session("year") = 96 
Set Session("myObj") = Server.CreateObject("someObj") 


Scripts for the following events are declared in the Global.asa file.



For more information about these events and the Global.asa file, see the Global.asa Reference.

© Microsoft Corporation. All rights reserved.

file: /Techref/language/asp/obj/introbj_38.htm, 8KB, , updated: 2013/11/1 15:08, local time: 2024/7/18 04:05,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> Session Object</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?


Welcome to!


Welcome to!