Cookie operating principles and advantages and disadvantages

Source: Internet
Author: User
Generally, the Cookie is returned from the server to the browser through HTTPHeaders. First, the server uses the Set-Cookieheader in the response to create a Cookie. then, the browser uses the Cookieheader to include the created Cookie in its request and returns the Cookie to the server, to verify the browser. Cookie storage method

Generally, the Cookie is returned from the server to the browser through HTTP Headers. First, the server uses the Set-Cookie header in the response to create a Cookie. then, the browser uses the Cookie header to include the created Cookie in its request, it is returned to the server to complete browser authentication.

Cookie storage method

Internet Explorer saves site cookies in the file name format @ In the. txt file, where Is your account name. For example, if your name is user and the site you visit is www.nowamagic.net, the site's Cookie is saved in a file named user@nowamagic.net.txt. (The file name may contain an sequential number, such as the user@nowamagic.net named 12.16.txt .) The Cookie text file is related to the user, so it will be saved separately according to the account.

Cookie restrictions

A Cookie occupies a basic space overhead of about 50 characters (used to save the validity period information, etc.). In addition, the length of the saved value is close to 4 K. Most browsers only allow 20 cookies to be saved on each site.

Why save the information to the cookie?

There are two solutions:

1. most servers use the session replication method: when the session value is changed, copy it to another machine. This solution has two specific implementations: one is the broadcast method, the disadvantage: when the traffic increases, the bandwidth increases, and as the number of machines increases, the network load increases exponentially, not highly scalable. The other method is TCP-ring, that is, to regard all servers in the cluster as A ring, A-> B-> C-> D->, end-to-end connections. Disadvantages: 1. complicated configurations; 2. when a machine is added/removed, the ring needs to be adjusted again, which will become a performance bottleneck; third, the front-end Load Balancer must be smart enough to distribute user requests to the correct machine.

2. Save the session in a single data source, which can be shared by all machines in the cluster, but the problem is the performance problem.

Solution: save the session on the client as a Cookie.

Advantage: high scalability and availability

  1. Through good programming, we can control the size of the session object stored in the cookie.
  2. Uses encryption and secure transmission (SSL) technology to reduce the possibility of cookie cracking.
  3. Store non-sensitive data only in cookies, and there will be no major loss even if the data is stolen.
  4. Control the life cycle of a cookie so that it will not always be valid. Attackers may obtain an expired cookie.

Disadvantages:

  1. The number and length of cookies. Each domain can have a maximum of 20 cookies. each cookie cannot exceed 4 kb in length. Otherwise, it will be truncated.
  2. Security issues. If the cookie is intercepted, the person can obtain all session information. Even if encryption is not completed, because the interceptor does not need to know the meaning of the cookie, he can simply forward the cookie as it is.
  3. Some statuses cannot be stored on the client. For example, to prevent repeated forms, we need to save a counter on the server side. If we save this counter on the client, it does not play any role.

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.