ASP Tutorial: Simple Learning to master ASP Session object

Source: Internet
Author: User
Tags change settings contains variables time interval
session| Objects | tutorials

The session object is used to store the user's information. Variables stored in the Session object hold information for a single user and are available for all pages in the application.

Session Object

When you're working on an application, you open it, make some changes, and then turn it off. It's like a conversation (session). The computer knows who you are. It's clear when you open and close the application. But there is a problem on the Internet: the Web server does not know who you are and what you do because the HTTP address cannot be persisted.

ASP solves this problem by creating a unique cookie for each user. The cookie is transmitted to the client, which contains information that is identifiable to the user. This interface is called a Session object.

The session object is used to store information about the user, or to change settings for a user session. Variables stored in the Session object hold information for a single user and are available for all pages in the application. The information stored in the session object is usually the name, ID, and parameters. The server creates a new session for each new user, and then revokes it when the session expires.

When does the session begin?

Session begins at:

    • When a new user requests an ASP file, and the Global.asa file references the Session_OnStart subroutine;
    • When a value is stored in the session variable;
    • When a user requests an ASP file, and Global.asa uses the <object> tag to sample an object through the scope of the session;

When does the session end?

If the user does not request or refresh the page in the application within the specified time, the session will end. The default value is 20 minutes.

You can set the Timeout property if you want to set the time interval for the timeout to be longer or shorter.

The following example sets a timeout interval of 5 minutes:

<%Session.Timeout=5%>

To end the session immediately, you can use the Abandon method:

<%Session.Abandon%>

Note: The main question when using session is when they should end. We do not know whether the user's most recent request is the last request. So we don't know how long it will take to "survive". Waiting too long for an idle session can deplete the server's resources. However, if the session is deleted prematurely, the user will have to restart again and again, because the server has deleted all the information. It is difficult to find the right timeout interval.

Tip: If you are using the session variable, store a small amount of data in it.

Storing and retrieving session variables

The greatest advantage of the session object is that it can be stored in a variable for subsequent Web page reads, with a wide range of applications.

The following example assigns "Donald Duck" to the session variable named username and assigns "50" to the session variable named Age:

<%session ("username") = "Donald Duck" session ("age") =50%>

Once the value is stored in the session variable, it can be used by any page in the ASP application:

Welcome <%response.write (Session ("username"))%>

The result of the above line is: "Welcome Donald Duck."

You can also save user parameters in the Session object and then decide what pages to return to the user by accessing these parameters.

The following example provides a text-only version of a page if the user uses a low display resolution:

<%if session ("screenres") = ' low ' then%> This was the text version of the page<%else%> this is the  mult Imedia version of the Page<%end if%>

Remove Session variable

The contents set contains all the session variables.

A session variable can be removed by using 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 ') <18 then  Session.Contents.Remove ("Sale") End If%>

To remove all variables in the session, use the RemoveAll method:

<%session.contents.removeall ()%>

Loop Traversal Contents Set

The contents set contains all the session variables. You can view the variables stored in them by traversing the contents set:

<%session ("username") = "Donald Duck" session (' age ') =50dim IFOR each i in session.contents  Response.Write (I & "<br/>") next%>

Results:

Usernameage

If you need to know the number of items in the contents set, you can use the Count property:

<%dim Idim Jj=session.contents.countresponse.write ("Session Variables:" & j) for I=1 to J  Response.Write ( Session.Contents (i) & "<br/>") next%>

Results:

Session Variables:2donald DUCK50

Loop Traversal StaticObjects Set

You can iterate through the StaticObjects set to view the values of all objects stored in the Session object:

<%dim IFOR each I in session.staticobjects  Response.Write (i & "<br/>") next%>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.