The principle and application of Session in ASP.

Source: Internet
Author: User
Tags set time

Session profile, features

1.Session is one of the common states in a Web session.

2.Session provides a way to keep information in the server's memory. He can store any data type, including a custom object.

3. The seesion of each client is stored independently.

4. During the entire session, as long as the SessionID cookie is not lost, the session information will be saved.

5.Session cannot be accessed across processes, only by users of that session. The ID identification of the session data should be saved in a cookie to the visitor's browser's cache.

6. When the session terminates, or expires, the server clears the session object.

7.Session is often used to save the ID of the logged-on user.

The data saved by 8.Session is a cross-page global type.

Use of Session

Here I write an example to facilitate the elaboration below.

 

 protected void Page_Load (object sender, EventArgs e) {//writes the user ID to the session if (request.for m["Hidlgclick"] = = "Lgclick") {if (request.form["Txtuid"]. ToString () = = "Admin" &&request.form["Txtuid"]. ToString () = = "admin")//Determine user login {session["userName"] = request.form["Txtuid"].  ToString (); Save the user ID to the Session Response.Write (session["UserName"). ToString () + "---Click to login"); Get session and write to page}}//Get session if (request.form["hidlgclick"] = = "Getse Ssion ") {if (session[" UserName "]! = null) {Response.Write (Se ssion["UserName"]. ToString () + "---Click to get Session");            Get session and write to page}}//Cancel current session, equivalent to logout (log out).            if (request.form["hidlgclick"] = = "Backlg") {Session.Abandon (); }        }
  <system.web>    <sessionstate timeout= "+" ></sessionState>  <!---set the session expiration time, Time is measured in minutes--

Session principle (as explained in the above example)

How is the session stored and extracted?

1. On the server side there is a session pool, used to store each user submits session data, session for each client (or browser instance) is a "hand", the first time the user to establish a connection with the Web server, The server distributes a sessionid as an identity to the user. SessionID is a random string of 24 characters. Each time the user submits the page, the browser will include this SessionID in the HTTP header to the Web server, so that the Web server can distinguish between the client on the current request page, and this sessionid is a cookie stored in the client's memory, If you want to get the data from the session pool, the server will give the corresponding data return based on the unique SessionID ID submitted by the client.

2. Enter the correct account password, click Login, the page will output "Admin---Click Login"

How is the data of each client in the session pool stored?

1. The data stored in the session pool is a global type of data that can be accessed across pages, with only the unique data stored in each SessionID, such as: First you set: session["UserName"]= "admin", Then you set: session["UserName"]= "123" If the session is not over yet, so the SessionID is not changed, but the data in the session pool is overwritten. At this point the value of session["UserName" is "123", not the other.

Data in a 2.Session pool cannot be accessed across processes. such as: Open login.aspx page Write session["userName"]= "admin"; then the login page does not close, that is, this session does not end, in this case you will open a Login.aspx page in another browser session[" UserName "]=null

3. Enter the account password, click the login page Output "Admin---Click Login", if you click to get the session button, then the page only output "Admin---Click Get Session", if the page does not close, open another browser, click the Get Session button , the page cannot be applied.

The declaration period and destruction of the session

1.session Storage Data timing is a scrolling timing method. Specifically, if you open the write session, from the beginning of writing, this page if there is no commit operation, the default time is 20 minutes, 20 minutes after the session is automatically destroyed by the server, if there is a commit operation, the server will be re-timed after the submission and so on, until the set time to destroy.

2. You can set the time for the session to be destroyed. The above code is mentioned.

Four, the session is stored in the data is on the server, and each user, such as login operation, the session data will be written, so it is recommended to use the session cautiously, is less use.

The principle and application of Session in ASP.

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.