Monday, March 22, 2010

ASP Session Object A Session object stores information about, or change settings for a user session.

________________________________________ Session Object When you are working with an application on your computer, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you open the application and when you close it. However, on the internet there is one problem: the web server does not know who you are and what you do, because the HTTP address doesn't maintain state. ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the user's computer and it contains information that identifies the user. This interface is called the Session object. The Session object stores information about, or change settings for a user session. Variables stored in a Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires. The Session object's collections, properties, methods, and events are described below: Collections Collection Description Contents Contains all the items appended to the session through a script command StaticObjects Contains all the objects appended to the session with the HTML tag Properties Property Description CodePage Specifies the character set that will be used when displaying dynamic content LCID Sets or returns an integer that specifies a location or region. Contents like date, time, and currency will be displayed according to that location or region SessionID Returns a unique id for each user. The unique id is generated by the server Timeout Sets or returns the timeout period (in minutes) for the Session object in this application Methods Method Description Abandon Destroys a user session Contents.Remove Deletes an item from the Contents collection Contents.RemoveAll() Deletes all items from the Contents collection Events Event Description Session_OnEnd Occurs when a session ends Session_OnStart Occurs when a session starts Collections: ASP Contents Collection ________________________________________ The Contents collection contains all the items appended to the application/session through a script command. Tip: To remove items from the Contents collection, use the Remove and RemoveAll methods. Syntax Application.Contents(Key) Session.Contents(Key) Parameter Description key Required. The name of the item to retrieve ________________________________________ Examples for the Application Object Example 1 Notice that both name and objtest would be appended to the Contents collection: <% Application("name")="W3Schools" Set Application("objtest")=Server.CreateObject("ADODB.Connection") %> Example 2 To loop through the Contents collection: <% for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> or: <% For i=1 to Application.Contents.Count Response.Write(i & "=" & Application.Contents(i) & " ") Next %> Example 3 <% Application("date")="2001/05/05" Application("author")="W3Schools" for each x in Application.Contents Response.Write(x & "=" & Application.Contents(x) & " ") next %> Output: date=2001/05/05 author=W3Schools ________________________________________ Examples for the Session Object Example 1 Notice that both name and objtest would be appended to the Contents collection: <% Session("name")="Hege" Set Session("objtest")=Server.CreateObject("ADODB.Connection") %> Example 2 To loop through the Contents collection: <% for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> or: <% For i=1 to Session.Contents.Count Response.Write(i & "=" & Session.Contents(i) & " ") Next %> Example 3 <% Session("name")="Hege" Session("date")="2001/05/05" for each x in Session.Contents Response.Write(x & "=" & Session.Contents(x) & " ") next %> Output: name=Hege date=2001/05/05 ASP StaticObjects Collection