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.
Copy codeThe Code is as follows: <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 a value to the hidden Control
}
</Script>
</Head>
<Body>
<Form id = "form1" method = "post" action = "MySession. aspx">
<Table>
<Tr>
<Td> account: </td> <input type = "text" name = "txtUid"/> </td>'
</Tr>
<Tr>
<Td> password: </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>
Copy codeThe Code is as follows: 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") // judge User Logon
{
Session ["userName"] = Request. Form ["txtUid"]. ToString (); // Save the user ID to the session.
Response. Write (Session ["userName"]. ToString () + "--- click Login"); // get the session and Write it to the page
}
}
// Obtain the Session
If (Request. Form ["hidlgclick"] = "getSession ")
{
If (Session ["userName"]! = Null)
{
Response. Write (Session ["userName"]. ToString () + "--- click to get session"); // obtain the session and Write it to the page
}
}
// Cancel the current session, which is equivalent to logging out ).
If (Request. Form ["hidlgclick"] = "backLg ")
{
Session. Abandon ();
}
}
Copy codeThe Code is as follows: <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 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.
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, click "admin" on the logon page, and click "Log On". If you click the "get session" button, only "admin" is displayed on the page. If the page is not closed, open another browser and click "Get session". The page cannot be accessed.
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. As mentioned in the code 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.