2025-04-27 07:49:33 -04:00

237 lines
9.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<TITLE>Session Object</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
TempString = navigator.appVersion
if (navigator.appName == "Microsoft Internet Explorer"){
// Check to see if browser is Microsoft
if (TempString.indexOf ("4.") >= 0){
// Check to see if it is IE 4
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else {
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
}
}
else if (navigator.appName == "Netscape") {
// Check to see if browser is Netscape
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/coua.css">');
}
else
document.writeln('<link rel="stylesheet" type="text/css" href="/iishelp/common/cocss.css">');
//-->
</script>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Window_OnLoad()
Dim frmContents
On Error Resume Next
If Not Parent Is Nothing Then
Set frmContents = Parent.Contents
If Not frmContents Is Nothing Then
frmContents.Window.TOCSynch_Click
End If
End If
End Sub
//--></SCRIPT><META NAME="DESCRIPTION" CONTENT="Internet Information Services reference information">
<META HTTP-EQUIV="PICS-Label" CONTENT='(PICS-1.1 "<http://www.rsac.org/ratingsv01.html>" l comment "RSACi North America Server" by "inet@microsoft.com <mailto:inet@microsoft.com>" r (n 0 s 0 v 0 l 0))'>
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="MS-IT-LOC" Content="Internet Information Services">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><A NAME="_session_object"></A><SUP></SUP>Session Object</H2>
<P>You can use the <B>Session</B> object to store information needed for a particular user-session. Variables stored in the <B>Session </B>object are not discarded when the user jumps between pages in the application; instead, these variables persist for the entire user-session. </P>
<P>The Web server automatically creates a <B>Session </B>object when a Web page from the application is requested by a user who does not already have a session. The server destroys the <B>Session </B>object when the session expires or is abandoned. </P>
<P>One common use for the <B>Session </B>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 <B>Session</B> object. For more information on using the <B>Session </B>object, see <A HREF="iiapsess.htm">Managing Sessions</A> in the ASP Applications section.</P>
<P><span class=le><B>Note</B></span>&nbsp;&nbsp;&nbsp;Session state is only maintained for browsers that support cookies.</P>
<H6>Syntax</H6>
<P><B>Session.</B><I>collection</I>|<I>property</I>|<I>method</I></P>
<P><I>&nbsp;</I></P>
<H6>Collections</H6>
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob8my6.htm"><B>Contents</B></A></TD>
<TD width=75%>Contains the items that you have added to the session with script commands. </TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob5ovi.htm"><B>StaticObjects</B></A></TD>
<TD width=75%>Contains the objects created with the &lt;OBJECT&gt; tag and given session scope.</TD>
</TR>
</TABLE><BR>
<H6>Properties</H6>
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob4ht1.htm"><B>CodePage</B></A></TD>
<TD width=75%>Sets the Codepage for data in the intrinsic objects for an entire session. The codepage tells the server how to encode characters for different languages.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob72uc.htm"><B>LCID</B></A></TD>
<TD width=75%>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.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob8b6s.htm"><B>SessionID</B></A></TD>
<TD width=75%>Returns the session identification for this user.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob04vo.htm"><B>Timeout</B></A></TD>
<TD width=75%>The timeout period for the session state for this application, in minutes.</TD>
</TR>
</TABLE><BR>
<H6>Methods</H6>
<TABLE border=0 cellpadding=5 cols=2 frame=box rules=all>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob5tpq.htm"><B>Abandon</B></A></TD>
<TD width=75%>This method destroys a <B>Session</B> object and releases its resources.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob155x.htm"><B>Contents.Remove</B></A></TD>
<TD width=75%>This method deletes an item from the <B>Contents</B> collection.</TD>
</TR>
<TR VALIGN="top">
<TD width=25%><A HREF="/iishelp/iis/htm/asp/vbob8bu4.htm"><B>Contents.RemoveAll</B></A></TD>
<TD width=75%>This method deletes all items from the <B>Contents</B> collection.</TD>
</TR>
</TABLE><BR>
<H6>Events </H6>
<P>Scripts for the following events are declared in the Global.asa file. </P>
<TABLE border=0 cellpadding=5 cols=1 frame=box rules=rows>
<TR VALIGN="top">
<TD width=100%><A HREF="/iishelp/iis/htm/asp/glob2y90.htm">Session_OnEnd</A></TD>
</TR>
<TR VALIGN="top">
<TD width=100%><A HREF="/iishelp/iis/htm/asp/glob6t84.htm">Session_OnStart</A></TD>
</TR>
</TABLE><BR>
<P>For more information about these events and the Global.asa file, see the <A HREF="/iishelp/iis/htm/asp/glob63vp.htm">Global.asa Reference</A>.</P>
<H6>Remarks</H6>
<P>You can store values in the <B>Session </B>object. Information stored in the <B>Session </B>object is available throughout the session and has session scope. The following script demonstrates storage of two types of variables.</P>
<PRE><CODE>&lt;%
Session("username") = "Janine"
Session("age") = 24
%&gt;
</CODE></PRE>
<P>However, if you store an object in the <B>Session</B> object and use VBScript as your primary scripting language, you must use the <B>Set</B> keyword. This is illustrated in the following script.</P>
<PRE><CODE>&lt;% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %&gt;
</CODE></PRE>
<P>You can then call the methods and properties exposed by <CODE>MyComponent.class1</CODE><I> </I>on subsequent Web pages, by using the following.</P>
<PRE><CODE>&lt;% Session("Obj1").MyMethod %&gt;
</CODE></PRE>
<P>Or by extracting a local copy of the object and using the following.</P>
<PRE><CODE>&lt;%
Set MyLocalObj1 = Session("Obj1")
MyLocalObj1.MyObjMethod
%&gt;
</CODE></PRE>
<P>Another way to create objects with session scope is to use the &lt;OBJECT&gt; tags in the Global.asa file. </P>
<P>You cannot, however, store a built-in object in a <B>Session </B>object. For example, each of the following lines would return an error.</P>
<PRE><CODE>&lt;%
Set Session("var1") = Session
Set Session("var2") = Request
Set Session("var3") = Response
Set Session("var4") = Server
Set Session("var5") = Application
%&gt;
</CODE></PRE>
<P>Before you store an object in the <B>Session </B>object, you should know what threading model it uses. Only objects marked as both can be stored in the <B>Session</B> object without locking the session to a single thread. For more information, see the Platform SDK.</P>
<P>If you store an array in a <B>Session</B> object, you should not attempt to alter the elements of the stored array directly. For example, the following script will not work:</P>
<PRE><CODE>&lt;% Session("StoredArray")(3) = "new value" %&gt;
</CODE></PRE>
<P>This is because the <B>Session</B> object is implemented as a collection. The array element <CODE>StoredArray(3)</CODE> does not receive the new value. Instead, the value is indexed into the collection, overwriting any information stored at that location. </P>
<P>It is strongly recommended that if you store an array in the <B>Session </B>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 <B>Session </B>object again so that any changes you made are saved. This is demonstrated in the following example:</P>
<PRE><CODE>---file1.asp---
&lt;%
'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")
%&gt;
---file2.asp---
&lt;%
'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)&amp;LocalArray(1))
'Re-storing the array in the Session object.
'This overwrites the values in StoredArray with the new values.
Session("StoredArray") = LocalArray
%&gt;
</CODE></PRE>
<H6>Example</H6>
<P>The following code assigns the string <CODE>MyName</CODE> to a session variable called <CODE>name</CODE>, assigns a value to a session variable called <CODE>year,</CODE> and assigns an instance of the <CODE>some.Obj</CODE> component to a variable called <CODE>myObj</CODE>. </P>
<PRE><CODE>Session("name") = "MyName"
Session("year") = 96
Set Session("myObj") = Server.CreateObject("someObj")
%&gt;
</CODE></PRE>
<P>For more information, see <A HREF="iiapsess.htm">Managing Sessions</A>.</P>
<hr class="iis" size="1">
<p align="center"><em><a href="../../../common/colegal.htm">&copy; 1997-2001 Microsoft Corporation. All rights reserved.</a></em></p>
</BODY>
</HTML>