|
Application
and Session Object
Scope of a session Object
As already discussed, the session object is available to user across pages of the application. A user object when a request is made to the
application for the first time by him or her. The session object and all objects or variables associated with it are kept alive until the session
times-out. A session times-out after a predefined predefined period of inactivity, which by default, is twenty minutes .This can be changed by setting a
Timeout property for the session object at runtime.
Using the session Object
The session Object provides a way to emulate a persistent connection across the client and the server. The events and methods of the session object are listed below:
Events
Session Onstart
Session OnEnd
Methods
Session Abandon
The Session OnStart event is like the load event of a visual basic application. it is invoked when a user makes the first request to the server. The event may be used
to user specific information. for example, the time of logging on checking the client for a cookie and
loading information from that , etc. The event invokes the script from the global.asa file. This script is written in the following manner;
<SCRIPT LANGUAGE =VBScript RunAt=Server>
Sub
Session_Onstart
'Code to execute when the sessionis started
End Sub
</SCRIPT>
The session OnEnd event is like the unload event of a visual basic form. The main different here is that the event is
mostly fired without user actions. In fact, the event is mostly fired because of user inactivity. The event is invoked just before the session is terminated, which occurs after a predefined period of inactivity, set by the sever of by the session timeout property. The event can be used to clean up object references and variables. The server will lose all information about the session after this
event. The script for the Session OnEnd event is written in the following manner:
<SCRIPT LANGUAGE=VBScript RunAt=Server>
Sub
Session_OnEnd
'Code to execute when the sessions started
End sub
</SCRIPT>
The session abandon method provides a mechanism to force a session to terminate before the predefined time. It may be used to explicity log off users. the syntax for this method is;
Session.Abandon
Properties of the Session Object
The session object has two intrinsic properties; the sessinoID Property and the session Timeout Property.
ASP creates the SessionID when the session itself is created. The SessionID is written as a long integer to a cookie on the client machine and provides the core of
tracking the user and related information. The SessionID can referenced form any page using the syntax:
Session.SessionID
The server stores the session Timeout property as a long integer that is interpreted in
minutes. The Timeout property can be altered at runtime. The syntax for this is:
Session.Timeout=<number of minutes>
For example, if you want to change the timeout to ten minutes. you would set
it this way;
Session.Timeout = 10
Apart from these properties, you can also add properties to the session object, just like the application object. This providesthe most improtant feature of the session object, since theseproperties can carry the user information across all the pages requested. The syntax for this is:
Session("propertyName")= value
The session object can be used to store data that the user inputs and that may be required over multiple pages. It may also be used to cache information from a database - when the user logs on, you can retrieve all his information from a database if it is stored there and access this
with never necessary. the result would be much faster than querying the database every time. It may also be used to manage a user's status; whether the user has signed in or not, whether he has been authenticated or not,
etc. Another application of the session object is with baskets- a download basket or a purchase order basket. for example. The basket
would store all items selected by the user which can be processed later when the user
finalizes his choices.
The contents of a session object
The session variables are stored in a collection of the session object name
contacts. As with all collections, you can use the count property to determine the number of items in the contents collection. You can also display all the items contained in the contents collection by using a for ... each or a For.. Next loop, as shown:
<%
Session("UserName") =
"DigitalNET Technolgies"
Session("Password")="SmartSurat"
Session("Title")="ASP"
%>
The session object now contain three properties, named UserName,
Password and Title.
<UL>
<%
For Each x In Session.Contents
Response.Write("<LI>" & x & " : " &
Session.Contents(x))
Next
%>
</UL>
<UL>
<%
For i=1 To Session.Contents.Count
Response.Write("<LI>" & Session.contents(i))
Next
%>
</UL>
Each of the above loops display the contents of the Session object. The main difference between these loops is that the former also display the name of the property that is listed.
|