Cookie: a small amount of data stored in the Client File System text file or client browser Dialog Memory. When a user requests a webpage, the application first checks whether the user has logged on to the webpage, we can read the Cookie to obtain user information to determine whether to allow it to continue accessing.
Record Cookie Information
Create a Cookie object named "user": HttpCookie cookie = new HttpCookie ("user ");
Assign values to cookies. Only strings can be used: Cookie. Value = "chenxiaomei ";
If you want to save multiple strings, you can do this as follows:
Cookie ["sex"] = "female ";
Cookie. Values. Add ("age", "18 ");
Read Cookie Information
Copy codeThe Code is as follows:
HttpCookie cookie = Request. Cookies ["user"];
If (null = cookie)
{
Response. Write ("No cookie found ");
}
Else
{
Response. Write ("All cookie values:" + cookie. Value + "<br/> ");
Response. Write ("sex value:" + cookie ["sex"] + "<br/> ");
Response. Write ("age value:" + cookie ["age"] + "<br/> ");
}
Delete Cookie
Because the Cookie is stored on the client, the browser can delete the Cookie for you. Set the Cookie value to the previous
A date.
Cookie. Expires = DateTime. Now. AddHours (-1 );
Session Object
When the user establishes a connection with the server for the first time, he creates a Session with the server, and the server will
It is automatically assigned a SessionID to identify the unique identity of the user.
Specific Session operations:
Copy codeThe Code is as follows:
// Store information
Session ["myname"] = "chenxiaomei ";
// Obtain information
String myname = Session ["myname"];
// Clear the session
Session. Clear (); // remove all keys and values from the Session state set
Session. Abandon (); // cancel the current Session
Difference between Session and Cookie:
The storage location of the information varies with the storage time.
Cookie is a text file stored by the webpage server on the hard disk of the webpage client. The webpage server sends a request to the webpage client to store a piece of information, which can be stored in cookies. When the client requests a page from the server, the information is sent back to the server.
Session variables will create a dictionary object for Each connection on the server, which is saved on the server. Cookie may have an expiration date determined by year, month, and day, and Session-level variables expire after connection timeout.