YouTip LogoYouTip

Asp Sessions

* * * The Session object is used to store information about a user session, or to change the settings of a user session. * * * ## Session Object When you work with an application on your computer, you open it, make some changes, and then close it. This is much like a Session. The computer knows who you are. It knows when you open and close the application. However, on the Internet there is a problem: the Web server does not know who you are or what you do, because the HTTP address cannot 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 can be used to identify the user. This interface is called the Session object. The Session object is used to store information about a user session, or to change the settings of a user session. Variables stored in the Session object store information about a single user, and are available to all pages in the application. The common information stored in session variables is name, id and parameters. The server creates a new Session for each new user, and destroys the Session object when the session expires. * * * ## When Does a Session Start? A session starts when: * A new user requests an ASP file, and the Global.asa file references the Session_OnStart subroutine * A value is stored in a Session variable * A user requests an ASP file, and Global.asa uses an tag to instantiate an object with session scope * * * ## When Does a Session End? A session ends if the user does not request or refresh a page within a specified time interval in the application. The default value is 20 minutes. If you want to set the timeout interval to be shorter or longer than the default value, you can use the **Timeout** property. The following example sets a 5-minute timeout interval: To end a session immediately, use the **Abandon** method: **Note:** The main problem with using sessions is knowing when they should end. We don't know if the user's most recent request was the last request. Therefore, we don't know how long to let the session "live". Waiting too long for an idle session can exhaust server resources. However, if the session is deleted too early, the user has to keep starting over and over because the server has deleted all the information. Finding the right timeout interval is very difficult! ![Image 1: Tip]( Only store small amounts of data in session variables! * * * ## Storing and Retrieving Session Variables The biggest advantage of the Session object is that you can store variables in it for subsequent pages to read, and its range of applications is very wide. The following example assigns "Donald Duck" to a Session variable named username and "50" to a Session variable named age: When values are stored in session variables, they can be used by any page in the ASP application: Welcome The result of the line above is: "Welcome Donald Duck". You can also store user parameters in the Session object, and then decide what page to return to the user by accessing these parameters. The following example specifies that if the user uses a low display resolution, the page returns a plain text version: This is the text version of the page This is the multimedia version of the page * * * ## Removing Session Variables The Contents collection contains all session variables. You can remove session variables via the Remove method. In the following example, if the value of the session variable "age" is less than 18, the session variable "sale" is removed: <% If Session.Contents("age") To remove all variables in the session, use the RemoveAll method: * * * ## Iterating Through the Contents Collection The Contents collection contains all session variables. You can iterate through the Contents collection to see the variables stored in it: <% Session("username")="Donald Duck" Session("age")=50 dim i For Each i in Session.Contents Response.Write(i & "
") Next %> Result: username age If you don't know how many items are in the Contents collection, you can use the Count property: <% dim i dim j j=Session.Contents.Count Response.Write("Session variables: " & j) For i=1 to j Response.Write(Session.Contents(i) & "
") Next %> Result: Session variables: 2 Donald Duck 50 * * * ## Iterating Through the StaticObjects Collection You can iterate through the StaticObjects collection to see the values of all objects stored in the Session object: <% dim i For Each i in Session.StaticObjects Response.Write(i & "
") Next %>
← Asp ApplicationsAsp Cookies β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.