Introduction to session in ASP. NET

Source: Internet
Author: User
Tags server memory

How the session works:

(1) When a session is enabled for the first time, a unique identifier is stored in a local cookie. (2) first use the session_start () function. php loads the stored session variables from the session repository. (3) When executing the PHP script, register the session variable by using the session_register () function. (4) When PHP script execution ends, session variables that have not been destroyed will be automatically stored in the session library under a certain local path. session in the INI file. save_path is specified. It can be loaded and used when you browse the Web page next time. What is session ??? There is a session pool on the server side to store the data in the session submitted by each user. The session is "one hand" for each client (or browser instance ", when a user establishes a connection with the Web server for the first time, the server will distribute a sessionid to the user as the identifier. Sessionid is a random string consisting of 24 characters. Each time a user submits a page, the browser will include this sessionid in the HTTP header and submit it to the Web server, so that the Web server can distinguish the client of the current request page, this sessionid is saved in the client memory in a cookie method. If you want to obtain the data in the session pool, the server returns the corresponding data based on the unique sessionid Id submitted by the client. The data stored in the session pool is global data and can be accessed across pages. Each sessionid only stores unique data. Data in the session Pool cannot be accessed across processes.

The Declaration cycle and destruction of the session, and the timer of the session storage data is a rolling timing method. This is the case. If you open the write session and the page is not submitted since the write, the default time is 20 minutes. After 20 minutes, the session will be automatically destroyed by the server, if a request is submitted, the server will re-timing the request and so on until it is destroyed within the specified time. You can set the session destruction time.

We know that the biggest difference between a session and a cookie is that all cookie information is stored on the client, and the session only stores an ID on the client as a token for verification with the server, the real data is stored in the server memory. The data stored in the session is stored on the server, and each user needs to write session data for login. Therefore, we recommend that you use the session with caution.

The following is an example I wrote. The general meaning of session is as follows:

This is a front-end page with a logon interface (for example), user name, password, and logon button. User Login requires the user name to be uploaded to the main interface, the display is the user login, I use session.

The above is the login interface, the user name is test, the main interface, the figure "test" is the user name, the value passed from the login interface.

Logon interface Background code (incomplete, cut !!) :

/// <Summary> /// log on to the main interface /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> protected void btnsign_in_click (Object sender, eventargs e) {string user = this.txt username. text. trim (); string Pwd = this.txt password. text. trim (); If (user = "" | Pwd = "") {response. write ("<SCRIPT> alert ('the user name and password cannot be blank. Enter the user name or password! '); </SCRIPT> ") ;}else {int state = 0; // operation database result string SQL = ""; // SQL statement try {// open the database helper. connect (); // query user information records SQL = "select username, password from usersinfo where username = '"+ User +"' and Password = '"+ PWD +"' "; // execute the query command state = helper. datatable_usersinfo (SQL); If (State> 0) {session ["username"] = this.txt username. text; // pass the value (User Name) server. transfer ("index. aspx ");} else {response. write ("<s Warning> alert ('Sorry, the user name or password is incorrect. Please enter it again! '); </SCRIPT> "); this.txt username. TEXT = ""; return ;}} catch (exception e) {e. message. tostring ();} finally {// close the database helper. close_up ();}}}

The session value in the login button

After a user logs in through the logon interface, the user's logon is displayed on the main interface. Therefore, the value transmitted from the logon interface should be loaded in the main page page_load. The Code is as follows:

       protected void Page_Load(object sender, EventArgs e)        {            if (Session["Username"].ToString() != null)            {                this.lblUsername.Text = Session["Username"].ToString();                Session.Timeout = 60;                //Session.Remove("Username");            }        }

In short, it is not very professional, but an example of self-practice. I have to say so. What are the specific functions? What are the problems and defects? They are not detailed. If you need this knowledge, try to find it online as much as possible...

 
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.