A detailed introduction to the session in ASP

Source: Internet
Author: User
Tags change settings
Session objects are used to store user information. The variables stored in the Session object hold information for a single user and are available for all pages in an application.

Session Object

When you manipulate an application, you open it, make some changes, and then close it. It's much like a conversation (session). The computer knows who you are. It knows when you open and close the application. But there is a problem on the Internet: because the HTTP address is not persisted, the Web server does not know who you are and what you have done.

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

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

When does the Session start?

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 using 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.

If you want to set the time-out interval to be longer or shorter, you can set the Timeout property.

The following example sets the time-out 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 the session is when they should end. We will not know if the user's most recent request is the last request. So we don't know how long it will take for the session to "survive". Waiting too long for an idle session will drain the server's resources. However, if the session is deleted prematurely, the user will have to start over and over again, because the server has deleted all the information. It is difficult to find a suitable time-out interval.

Tip : If you are using session variables, do not store large amounts of data in them.

Storing and retrieving session variables

The biggest advantage of the Session object is that it can store variables for subsequent pages to be read, and its application scope is wide.

The following example assigns "Donald Duck" to a session variable named username and assigns "50" to a 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 this line of procedure is: "Welcome Donald Duck".

You can also save user parameters in the Session object and then access these parameters to determine what page to return to the user.

The following example specifies that a plain text version of a page is returned if the user is using a low display resolution:

<%if Session ("screenres") = "Low" then%> This is the text version of the page<%else%> this is the   Multim Edia version of the Page<%end if%>

removing session Variables

The Contents collection contains all the session variables.

The Remove method can be used to remove the session variable.

In the following example, if the value of the session variable "age" is less than 18, then 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 ()%>

Traversing the Contents collection

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

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

Results:

Usernameage

If you need to know the number of items in the Contents collection, 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

Traversing the StaticObjects collection

You can view the values of all objects stored in the session object by looping through the StaticObjects collection:

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

"Recommended"

1. asp free Video Tutorial

2. Teach you how to solve the loss of ASP session

3. Three ways to introduce session objects in ASP

4. Detailed use of Session in ASP

5. ASP Session Simple Example

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.