Asp. NET state management five (cookies)

Source: Internet
Author: User
Tags root directory

Cookies provide a way to store user-specific information in a WEB application.

For example, when a user accesses your site, you can use cookies to store user preferences or other information. When the user accesses your Web site again, the application can retrieve previously stored information.

What is a Cookie?

A Cookie is a small piece of textual information that accompanies the user request and the page is passed between the WEB server and the browser.

Cookies contain information that the WEB application can read every time a user accesses the site.

For example, when a user requests a page in a Web, the application sends the user more than one page and a cookie containing the date and time, and the user's browser obtains the cookie and stores it in a folder on the user's hard disk.

Later, if the user requests a page in your site again, when the user enters a URL, the browser looks for the Cookie associated with the URL on the local hard disk. If the cookie exists, the browser sends the cookie to your site along with the page request. The application can then determine the date and time that the user last visited the site. You can use this information to display a message to the user or to check the expiration date.

Cookies are associated with a Web site, not with a particular page. Therefore, the browser and the server Exchange Cookie information regardless of which page in the site the user is requesting. When users visit different sites, each site may send a cookie to the user's browser, and the browser stores all cookies separately.

Cookies Help the Web site store information about visitors. In general, cookies are a way of keeping WEB application continuity (that is, performing state management). The browser is disconnected from the WEB server, except for a short period of actual exchange of information. For each request that a user makes to a Web server, the Web server handles it separately. However, in many cases, it is useful for a WEB server to identify users when they request a page. For example, a Web server on a shopping site tracks every shopper so that the site can manage shopping carts and other user-specific information. As a result, cookies can serve as a business card that provides relevant identification information to help the application determine how to proceed.

Cookies can be used for a variety of purposes, all of which are designed to help the site remember users. For example, a site that implements a poll can simply use the Cookie as a Boolean value to indicate whether the user's browser has participated in the vote, so that the user cannot vote for the second time. A site that requires a user to log on can use a Cookie to record that the user is logged in, so that the user does not have to enter credentials each time.

Limitations of cookies

Most browsers support cookies with a maximum of 4096 bytes. Because this limits the size of cookies, it is best to use cookies to store small amounts of data, or to store identifiers such as user IDs. The user ID can then be used to identify the user and to read user information from a database or other data source. (For information on storing security recommendations for user information, see the "Cookies and Security" section below.) )

Browsers also limit the number of cookies that a site can store on a user's computer. Most browsers allow only 20 cookies per site, and if you try to store more cookies, the oldest cookies are discarded. Some browsers also have an absolute limit of the total number of cookies they will accept from all sites, usually 300.

The cookie limit you may encounter is that users can set their browser to reject cookies. If you define a P3P privacy policy and place it in the root directory of your Web site, more browsers will accept cookies from your site. However, you may have to completely discard cookies and use other mechanisms to store user-specific information.

Related Article

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.