Session Object
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 (timeout) or is abandoned.
You can use the Session object to store information needed for 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.
One common use 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.
Note Session state is only maintained for browsers that support
cookies.
Syntax:
Session.collections|property|method
Collections
Contents: Contains the items that your have added
to the session with script commands
StaticObjects: Contains the objects created
with the tag and given session scope.
Properties
CodePage: The codepage that will be used for symbol mapping. It
is an integer, read/write. The codepage is the numeric value of the
character set, and different languages and locales may use different
code pages. For example, ANSI codepage 1252 is used for American
English.
LCID: The locale identifier. The LCID is a standard international
abbreviation that uniquely identifies the locale.
SessionID: returns the session identification for
this users, which is generated by the server when the session is
created.
Timeout: The timeout period for the session state
for this application, in minutes.
Methods
Abandon: This method destroys a Session object and releases its
resources.
Events:
Scripts for the following events are declared in the global.asa
file.
Session_OnEnd
Session_OnStart
Remarks
You can store values in the Session object. Information stored in
the Session object is available throughout the session and has session
scope.
Example: Session variable
Saving the following code to SessionTest.asp, refresh the page couple
of times. Each time you should see different output.
SessionTest.asp
<%
dim tAccessTime
tAccessTime=Session("LastAccessTime")
if tAccessTime="" then
Response.Write("This is the first time this page has been accessed!")
else
Response.Write("This page was last accessed at " & tAccessTime
)
end if
%>
<% Session("LastAccessTime")=Now%>
Declaring Session Objects in global.asa
If you sore an object in the Session object and use VBScript as
your primary scripting language, you must use the Set keyword. This
is illustrated in the following script.
<% Set Session("Obj1") = Server. CreateObject("MyComponent.class1")%>
You can then call 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
%>
Storing Arrays in the Session Object
As with the Application Object, you should not set Array values
directly in the Session object. The array should be copied to a local
variable, assignment done and then the entire array copied back to
the Session Object. Please referrer on "Storing arrays in the
Application Object"
|