Cookie creation, read/write, and deletion

Source: Internet
Author: User
What is a cookie?

Cookie is a short text message that is transmitted between the Web server and the browser as user requests and pages. Cookie contains the web application each time a user accesses the siteProgramInformation that can be read.

For example, if the application sends a page to the user when the user requests the page in the site, it is not only a page, but also a cookie containing the date and time, the user's browser obtains the cookie while obtaining the page, and stores it in a folder on the user's hard disk.

Later, if the user requests the page on your site again, when the user enters the URL, the browser will find the cookie associated with the URL on the local hard disk. If the cookie exists, the browser sends the cookie along with the page request to your site. Then, the application can determine the date and time of the user's last visit to the site. You can use this information to display a message to the user or check the expiration date.

Cookies are associated with websites rather than specific pages. Therefore, no matter which page on the site the user requests, the browser and server exchange cookie information. When a user accesses different sites, each site may send a cookie to the user's browser, which stores all the cookies separately.

Cookies help websites store information about visitors. In general, cookie is a way to maintain the continuity of Web applications (that is, execution status management. Except for a short period of actual information exchange time, the browser and the Web server are disconnected. The Web server processes each request sent to the Web server separately. However, in many cases, it is very useful for Web servers to identify users on user request pages. For example, a Web server on a shopping site tracks each shopper so that the site can manage the shopping cart and other user-specific information. Therefore, Cookie can be used as a business card to provide relevant identification information to help the application determine how to continue execution.

Cookie can be used for multiple purposes, all of which are to help websites remember users. For example, a site that implements a public opinion test can simply use cookies as a Boolean value to indicate whether the user's browser has participated in the vote, so that the user cannot perform a second vote. Websites requiring users to log on can use cookies to record that users have logged on, so that users do not have to enter creden。 every time.

Cookie restrictions

Most browsers support a cookie of up to 4096 bytes. Because this limits the cookie size, it is best to use cookies to store a small amount of data or user IDs. User IDs can then be used to identify users and read user information from databases or other data sources.

The browser also limits the number of cookies that the site can store on the user's computer. Most browsers only allow 20 cookies per site. If you try to store more cookies, the oldest cookies will be discarded. Some browsers impose absolute limits on the total number of cookies they will accept from all sites, typically 300.

The cookie restriction you may encounter is that users can set their browsers to reject cookies. If you define an p3p Privacy Policy and place it in the root directory of the website, more browsers will accept the cookies of your site. However, you may have to discard cookies completely and store user-specific information through other mechanisms. Session status depends on cookies.

Compile cookie

The browser manages cookies in the user's system. Cookie is sent to the browser through the httpresponse object, which is known as a set of cookies. The httpresponse object can be accessed as the response attribute of the page class. All cookies to be sent to the browser must be added to this collection. When creating a cookie, you must specify the name and value. Each Cookie must have a unique name so that it can be recognized when being read from the browser. Because cookies are stored by name, naming two cookies with the same name causes one cookie to be overwritten.

You can also set the cookie expiration date and time. When a user accesses the website where the cookie is written, the browser deletes the expired cookie. As long as the application considers the cookie value valid, the cookie validity period should be set to this period. For cookies that never expire, you can set the expiration date to 50 years from now on.

Cookie Validity Period

If no cookie validity period is set, a cookie will still be created, but it will not be stored on the user's hard disk. The cookie is maintained as part of the user's session information. When the user closes the browser, the cookie is discarded. This kind of non-permanent cookie is suitable for saving the information stored for a short period of time, or saving the information that should not be written to the disk on the client computer for security reasons. For example, if you are using a public computer and you do not want to write cookies to the disk of the computer, you can use non-permanent cookies.

Read, write, and delete cookies
   Stringbuilder sb = New Stringbuilder ();// Httpcookie cookie = new httpcookie ("datecookieexample "); Httpcookie cookie = request. Cookies. Get (" Datecookieexample "); If (Cookie = Null ) {Sb. append (" Cookie was not encoded ed from the client. <br/> "); Sb. append (" Creating cookie to add to the response. <br/> "); // Create a cookie. Cookie = New Httpcookie (" Datecookieexample "); // Set the cookie value to the current time. Cookie. value = datetime. Now. tostring (); // Set the cookie expiration time. Cookie. expires = datetime. Now. addminutes (10d ); // Insert the cookie in the current httpresponse. Response. Cookies. Add (cookie );} Else { // Read cookie SB. append (" Cookie retrieved from client. <br/> "); Sb. append (" Cookie Name: "+ Cookie. Name +"<Br/> "); Sb. append (" Cookie value: "+ Cookie. Value +" <Br/> "); Sb. append (" Cookie expiration date: "+ Cookie. expires. tostring () +" <Br/> "); // Delete the cookie Cookie. Values. Clear (); cookie. expires = datetime. Now. addyears (-1); httpcontext. Current. response. appendcookie (cookie );}


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.