Session Introduction and features
1. session is one of the common states in Web sessions.
2. session provides a way to store information in the server memory. It can store any data type, including custom objects.
3. the seesion of each client is stored independently.
4. The session information will be saved as long as the cookie of the sessionid is not lost throughout the session.
5. The session cannot be accessed across processes and can only be accessed by the user of the session. The ID used to extract session data should be saved to the cache of the visitor's browser as a cookie.
6. When the session ends or expires, the server clears the session object.
7. Session is often used to save the Login User ID.
8. The data stored by the session is global across pages.
Session usage
Here I will write an example to illustrate it below.
< Head Runat = "Server" > < Title > </ Title > < Script SRC = "Scripts/jquery-1.4.1.min.js" Type = "Text/JavaScript" > </ Script > < Script Type = "Text/JavaScript" > Function Getsessionclick (Action ){ // This function is used to know which commit button is clicked. $ ( " # Hidlgc " ). Val ( "" ); // Clear hidden values $ ( " # Hidlgc " ). Val (action ); // Assign values to hidden controls } </ Script > </ Head > < Body > < Form ID = "Form1" Method = "Post" Action = "Mysession. aspx" > < Table > < Tr > < TD > Account: </ TD > < TD > < Input Type = "Text" Name = "Txtuid" /> </ TD > ' </ Tr > < Tr > < TD > Password: </ TD > < TD > < Input Type = "Password" Name = "Txtpwd" /> </ TD > </ Tr > < Tr > < TD Colspan = "2" > < Input Type = "Hidden" Value = "" ID = "Hidlgc" Name = "Hidlgclick" /> < Input Onclick = "Getsessionclick ('lgclick ')" Type = "Submit" Value = "Login" /> < Input Type = "Submit" Onclick = "Getsessionclick ('getsession ')" Value = "Get session" /> < Input Type = "Submit" Onclick = "Getsessionclick ('backlg ')" Value = "Log out" /> </ TD > </ Tr > </ Table > </ Form > </ Body >
Protected Void Page_load ( Object Sender, eventargs e ){ // Write the user ID to the session If (Request. Form [ " Hidlgclick " ] = " Lgclick " ){ If (Request. Form [ " Txtuid " ]. Tostring () =" Admin " & Request. Form [ " Txtuid " ]. Tostring () = " Admin " ) // Determine User Logon {Session [ " Username " ] = Request. Form [" Txtuid " ]. Tostring (); // Save the user ID to the session Response. Write (session [ " Username " ]. Tostring () + " --- Click to log on " ); // Get the session and write it to the page }} // Get session If (Request. Form [ " Hidlgclick " ] = " Getsession " ){ If (Session [ " Username " ]! = Null ) {Response. Write (session [ " Username " ]. Tostring () + " --- Click to get session " ); // Get the session and write it to the page }} // Canceling the current session is equivalent to logging out (logging out ). If (Request. Form [ " Hidlgclick " ] = " Backlg " ) {Session. Abandon ();}}
<System. Web> <sessionstate timeout ="40"> </Sessionstate> <! --- Set the session expiration time, in minutes -->
Session principle (based on the above example)
1. How is session stored and extracted?
1. There is a session pool on the server to store the data in the session submitted by each user.The session is a "manual copy" 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.
2. Enter the correct account password and click log on. The "Admin-Click Log On" page is displayed"
2. How is the data stored by each client in the session pool?
1. the data stored in the session pool is global data and can be accessed across pages. Each sessionid only stores unique data. For example, you can set it as follows: session ["username"] = "admin", and then when the session that has not ended has not expired, you set: session ["username"] = "123"; in this way, the sessionid is not changed, but the data in the session pool is overwritten. In this case, the value of session ["username"] is "123" instead of other values.
2. Data in the session Pool cannot be accessed across processes. For example, open login. on the ASPX page, write the session ["username"] = "admin"; then the login page is not closed, that is, the session is not terminated. In this case, you can open a login in another browser. on the ASPX page, session ["username"] = NULL
3. Enter the account password and click the login page to output"Admin --- click Login". If you click the get session button next to it, only "Admin --- click get session" will be output on the page. If the page is not closed, open another browser, if you click the get session button, the page cannot be applied.
Iii. Session Declaration cycle and destruction
1. session data storage timing 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.
2. You can set the session destruction time. AboveCodeMentioned above.
IV. 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, that is, less use.