Introduction of three methods of Session object in ASP

Source: Internet
Author: User
In ASP, there are two internal objects that can store information, which are application objects and session objects, where the Application object is for the entire application period and is shared for all users who access the site, and the session is for the duration of the session, it exists only with the current user.

Introduction to Session Objects

When you manipulate an application on a computer, you open it, make some changes, and then close it, much like a conversation (Session). The computer knows who you are, and it knows when you open and close the application. However, the problem arises on the Internet, and because the HTTP address cannot remain in the state, the Web server does not know who you are and what you have done.

The main purpose of the Session object is to store some information for each user who accesses the site, for example, when a user logs in, we can store information in the user's Session to identify that the current user is logged in.

The principle of the Session is that when the user first visits the site, IIS assigns an identity to the user, which is a long, random string called SessionID, The server then sends it to the client during the time it is saved in the cookie, the next time the user accesses the other pages on the server, the server obtains the SessionID, and the SessionID related data from memory is placed in the Session object collection.

Contents Collection

We can store some information about the current user in this collection, such as the following code showing how to store and read data:

A "Zhangsan" string session.contents ("username") = "Zhangsan" is stored in the <% ' session set named username UserName ' reads the data in the Session, you can omit Contents ' UserName = session.contents ("UserName") and the following UserName = Session ("UserName") Response.Write ("

The session object has three methods (Contents.Remove, Contents.RemoveAll, Abandon) to delete the data in the session collection or to discard the current session.

A first example (sessioncontents.asp) We will show you how to use the Remove, RemoveAll method with the following code:

...

The above code is run and appears as follows:

This code demonstrates two ways to traverse the Session.Contents collection, so take a closer look.

The second example (sessionabandon.asp) demonstrates the effect of the Abandon method, and through the demonstration, we see that the difference between the RemoveAll method and the Abandon method is that RemoveAll simply removes the current collection. But the customer still uses the same SessionID (in the first example, SessionID remains unchanged). After the Abandon method is called, the session collection can still be accessed on the current page, and the previous session will be deleted after the page is closed or the page is refreshed (SessionID in this example will change).

The code is as follows:

<% ' abandon is used, the session collection can still be accessed on the current page, When you close the page or refresh it, the session will be deleted and SessionID will change the session. Abandon () ' First we need to record the value of SessionID, store it in the cookie Dim numvisits, Sidresponse.cookies ("NumVisits"). Expires = DATEADD ("D", ten, Now) Response.Cookies ("SID"). Expires = DATEADD ("D", ten, now) SID = Request.Cookies ("Sid") NumVisits = Request.Cookies ("NumVisits") If NumVisits = "" or SI D = "Then" If this is the first time the page is run, the current Sessio nid value Response.Cookies ("numvisits") = 1 Response.Cookies ("SID") = Session.SessionID %> 

The first time you run this page, you will first record the current SessionID into the cookie, such as:

Refresh the page more than once or reopen it, because the Abandon method will cause the Session to be deleted, SessionID will always change, such as:

CodePage, SessionID, Timeout properties

The CodePage property defines the character set for the current page output, where the character set is represented numerically. For example

936 = Chinese Simplified (GB2312) Simplified Chinese

950 = Chinese Traditional (Big5) Traditional Chinese

65001 = Unicode (UTF-8)

Special Instructions

<% @CODEPAGE = "65001"%> acts on all static strings
Response.CodePage, Session.CodePage action on all dynamic output strings
The Response.CodePage scope is only a single Response
Session.CodePage is scoped to all responses in a session

The SessionID property can get the current user's SessionID, and sometimes you can attach SessionID to the client's QueryString variable in case the client browser does not support Cookies, thereby identifying each client.

The timeout attribute is used to set the client's Session timeout period. The client is not a long-term possession of SessionID, and after a period of time without any interaction with the server, the server will discard the Session.

The following code (SESSIONCST.ASP) will demonstrate the use of this three property, with the following code:

<% @LANGUAGE = "VBSCRIPT" codepage= "65001"%><%session.codepage = 65001 ' strings acting on all dynamic outputs%><!doctype HTML >

After running, the effect is as follows:

"Recommended"

1. asp free Video Tutorial

2. Detailed use of Session in ASP

3. ASP Session Simple Example

4. A detailed introduction to the session in ASP

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

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.