Platform SDK: IIS SDK |
[IIS 5.0] [IIS 5.1] [IIS 6.0]
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. For example, if a user indicates that they prefer not to view graphics, you could store that information in the Session object. For more information about using the Session object, see Managing Sessions in the Active Server Pages & Web Application Guide section.
Note Session state is only maintained for browsers that support cookies.
Session. collection|property|method
Session.Contents Collection | Contains the items that you have added to the session by using script commands. |
Session.StaticObjects Colleciton | Contains the objects created by using the <OBJECT> tag and given session scope. |
Session.CodePage | Sets the codepage for data in the intrinsic objects for an entire session. The codepage specifies to the server how to encode characters for different languages. |
Session.LCID | Sets the LCID for data for an entire session. The LCID refers to how dates, times, and currency are formatted for a specific geographical locale. |
Session.SessionID | Returns the session identification for this user. |
Session.Timeout | The time-out period for the session state for this application, in minutes. |
Session.Abandon | This method destroys a Session object and releases its resources. |
Session.Contents.Remove | This method deletes an item from the Contents collection. |
Session.Contents.RemoveAll | This method deletes all items from the Contents collection. |
Scripts for the following events are declared in the Global.asa file.
Session_OnEnd Event |
Session_OnStart Event |
For more information about these events and the Global.asa file, see the Global.asa Reference.
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 VBScript as your primary scripting language, you must use the Set keyword, as illustrated in the following script.
<% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %>
You can then use the methods and properties exposed by
MyComponent.class1
on subsequent Web pages, by using the following:
<% Session("Obj1").MyMethod %>
Or by extracting a local copy of the object and using the following:
<%
Set MyLocalObj1 = Session("Obj1")
MyLocalObj1.MyObjMethod
%>
Another way to create objects with session scope is to use the <OBJECT> tags in the Global.asa file.
You cannot, however, store a built-in object in a Session object. For example, each of the following lines returns 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 can be stored in the Session object without locking the session to a single thread.
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 does not work:
<% Session("StoredArray")(3) = "new value" %>
The preceding script does not work 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 finished using the array, you should store the array in the Session object again, so that any changes you made are saved, as demonstrated in the following example:
--- File1.asp ---
<%
'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
Response.Redirect("file2.asp")
%>
--- File2.asp ---
<%
'Retrieving the array from the Session Object
'and modifying its second element.
LocalArray = Session("StoredArray")
LocalArray(1) = " there"
'Printing out the string "hello there."
Response.Write(LocalArray(0)&LocalArray(1))
'Re-storing the array in the Session object.
'This overwrites the values in StoredArray with the new values.
Session("StoredArray") = LocalArray
%>
The following code assigns the string "MyName" to a session variable named name, assigns a value to a session variable named year, and assigns an instance of the some.Obj component to a variable named myObj:
<%
Session("name") = "MyName"
Session("year") = 96
Set Session("myObj") = Server.CreateObject("someObj")
%>
Platforms: Windows 2000 with IIS 5.0 installed, Windows XP
with IIS 5.1 installed, Windows Server 2003 family with IIS 6.0
installed
What
did you think of this topic? |
Order a Platform SDK CD |
file: /Techref/language/asp/OBJ/ref_vbom_seso.htm, 10KB, , updated: 2005/9/30 13:44, local time: 2024/11/26 17:09,
3.16.69.243:LOG IN
|
©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? <A HREF="http://massmind.ecomorder.com/techref/language/asp/OBJ/ref_vbom_seso.htm"> Session Object</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to massmind.ecomorder.com! |
.