What is Cookie?

Source: Internet
Author: User
Tags website server

tag: http OS file data I/O for art

I. Cookie introduction, understanding what is Cookie 1. What is Cookie: cookie is a kind of method that allows the website server to store a small amount of data (about 4 kb) to the client's hard disk or memory. And read can be obtained from a technology. 2. when you browse a website, the web server places a very small text file on your hard disk, it can record the information that the website wants to save, such as your user ID, browsed web page, or stay time. When you access the website through a browser again, the browser will automatically send the cookies belonging to the website to the server, and the server will read the cookies to learn your related information, you can make corresponding actions. For example, you are welcome to the title, and you do not need to enter the account password to log on directly .. 3. Different browsers store different cooks locations. The information in the cookie file is insecure, so it is best to encrypt the data in the cookie. 4. the browser saves cookie data in the form of 2: memory in the browser, and hard disk on the computer where the browser is located. II. how to view the existence of a cookie in a hard disk: 3. cookie code explanation 1. write the cookie to the browser and copy the Code as follows: httpcookie cookie = new httpcookie ("ID", "234"); // create a cookie instance. Response. cookies. add (cookie); // enter the cookie file created in the browser to explain: This is equivalent to writing the key-Value Pair ID: 234 in the cookie file. We can read this data. 2. the copy code of the data stored in the read cookie is as follows: httpcookie cookie = new httpcookie ("ID", "234"); // create a cookie instance. Response. cookies. add (cookie); // enter the created cookie file to the browser response. write (request. cookies ["ID"]. value); // read the value stored in the cookie file. explain: the data written on the page is 234. here we can see that the cookie is insecure. Therefore, it is best not to store important information when using it. If you want to store it, You can encrypt it and write it into the cookie storage file. In addition, if there is no limit to writing to it, too many junk files will be generated. Therefore, we can add a validity period to the cookie file. 3. Copy the code for setting the cookie file validity period: httpcookie cookie = new httpcookie ("ID", "234"); // create a cookie instance. Cookie. expires = datetime. now. addmonths (5); // sets the cookie expiration time. After 5 minutes, the file response is automatically cleared. cookies. add (cookie); // enter the created cookie file to the browser response. write (request. cookies ["ID"]. value); // read the value stored in the cookie file. the copy code for deleting and destroying cookie files is as follows: httpcookie cookie = new httpcookie ("ID", "234"); // create a cookie instance. Cookie. expires = datetime. now. addmonths (5); // sets the cookie expiration time. After 5 minutes, the file response is automatically cleared. cookies. add (cookie); // enter the created cookie file to the browser response. write (request. cookies ["ID"]. value); // read the value cookie stored in the cookie file. expires = datetime. now. addmonths (-5); // when the cookie is destroyed, set an extra time for the cookie to be destroyed. 4. A small cookie example, remember me. (If you select remember me when logging on, you do not need to log on to the content page next time.) copy the Code as follows: <body> <Form ID = "form1" method = "Post" Action = "rembpage. aspx "> <div> account: <input type =" text "name =" username "/> <br/> password: <input type = "password" name = "pass"/> <br/> remember me: <input type = "checkbox" value = "REM" name = "sele1"/> <br/> <input type = "Submit" value = "Logon"/> </Div> </form> </body> protected void page_load (Object sender, eventargs e) {If (request. cookies ["Username"] = NULL & request. Cookies ["password"] = NULL) {If (request. Form ["username"]! = NULL & request. Form ["pass"]! = NULL) {string username = request. form ["username"]; string userpassword = request. form ["pass"]; If (username = "admin" & userpassword = "123") {If (request. form ["sele1"]! = NULL) {httpcookie cookieusername = new httpcookie ("username", username); // create an account's cookie instance httpcookie cookiepassword = new httpcookie ("password", userpassword); cookieusername. expires = datetime. now. adddays (2); // set the account cookie expiration time. The current time is counted as two days later. expires = new datetime (2012, 5, 27); // set the password cookie expiration time, which is May 27, 2012 response. cookies. add (cookieusername); // enter the created cookieusername file to the browser's response. cookies. add (cookiepassword); response. redirect ("1. aspx "); // jump to the page you want} else {response. redirect ("1. aspx "); // jump to the else {response even if you do not remember the password. redirect ("1. aspx "); // if you remember the password, the second login will go directly to 1. ASPX page} detailed source reference: http://www.jb51.net/article/30398.htm

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.