Analysis of the principle and phenomenon of cookies

Source: Internet
Author: User
Tags setcookie
when it comes to cookies, you have to start with the HTTP protocol. what is the HTTP protocol? A single literal understanding, agreement, Convention, there must be at least two participants, communication between the participants must follow such a specification.

It is like white clouds and black soil two people together, the two people after the unified communication with the two agreed to the language to communicate, white clouds out of the left hand, then the black soil to see this action will make judgments, oh, the original is to beat the leg, so immediately jumped up to whack leg. Baiyun stretched right hand, black soil a look, mom Ah, this is to drink water rhythm, so immediately handed over, at this time white clouds out left foot, black soil immediately kissed a mouthful. The communication between the two people is because they follow the agreement between each other, so it becomes easy to understand and execute efficiently.

The HTTP protocol is also the same as the participants in the Protocol are the client (Baiyun) and the server side (black soil).

It is with the HTTP protocol, the communication between the client and the server has become easy to understand, imagine, if the white clouds and black soil in advance in accordance with the Convention to standardize their communication methods, the white clouds out of the left foot, estimating the black Uncle thought to kick himself.

Next, let's look at the features of the HTTP protocol.

1, support customer/server mode;

2, simple and fast

3. Flexible

4. No connection

5, no State.

Support client/server-side mode: Simply speaking, support two people communication, that is, support white clouds and black soil two people talk to each other.

Simple and convenient: like white clouds and black soil communication, stretching the left hand, stretching the right hand so simple. HTTP protocol is the same, whether it is the client request or service side response, set the corresponding message header, method, message body, can be communicated to the other side, and both can understand the meaning of each other.

The reason for this flexibility is that HTTP allows the transfer of any type of data object. The type of transmission is marked by Content-type.

No connection refers to the client-side request server, which automatically disconnects the link after the service-side response is completed. Just like the white clouds stretched his left hand, the black soil came over to hammer the legs, the hammer finished automatically go away to busy other.

Stateless refers to the client request server, which does not log any data for this connection communication after the server response is completed. Like the white clouds stretched his left hand, the black soil came to hammer legs, hammer finish leg, you ask the Black uncle, you beat the legs when all said something, the black soil replied: I do not remember anything.

Of course, the HTTP protocol is much more than that, specifically, we will say later, with the above HTTP protocol knowledge, we are going to elicit our cookies.

mentioned above, the white cloud and the black soil agreed to their own communication, very efficient and very simple, but their communication also has a certain problem, that is, the state of the HTTP protocol, that is, the black uncle to the white Cloud hammer leg when do not know what two people said, his head is not good, remember, only remember the individual things, This is not a good thing, for example, when beating the legs, white clouds told the black soil, your hands are too big, beating me good pain, light, the head of the black uncle Heard, immediately hand to put a little bit, and then go away to busy other. The black soil just walk, white clouds and stretched out the left hand, the black soil hurriedly come over to whack the leg, or heavy pounding legs, Baiyun Jiyan, just not told you? Hand put a light point, whack the mother's legs are numb, black uncle a face Meng forced, pro, I really do not remember.

What to do?

According to this, the black uncle to the white clouds to die, so someone put forward a proposal, white clouds each hand or leg when throwing out a note, the note above some of her personal requirements, the black uncle to get a note, while watching the note run to make the corresponding response to the operation, the response to the completion of the note back to the white clouds, In order to let Baiyun next time to find him can write the request at the same time to throw this note to the black soil.

Although some of this method is not elegant, but also to solve the black uncle can not remember anything.

, the above note is actually the cookie we are going to talk about today, when the client initiates a request to the server, the client takes a cookie to request the service side, and with this cookie, it can remember the status of the last request and related information, so that the communication has a state and memory.

Many people do not recommend the use of cookies when storing some data, because every time the client requests the server, it will bring cookie information, although the cookie is usually a value of only 4 K size, but once the volume comes up will be very bandwidth.

Then let's explain some of the basic working principles of cookies:

Cookies are mainly passed through the header Httpheader to pass data. That is, the data is passed in the request header and the message header. The cookie mechanism defines two types of headers: the Set-cookie header and the cookie header. The Set-cookie exists in the message header of the server response. The cookie exists in the message header of the client request.

The operating procedure for cookies is as follows:



(1) The client type the URL of the Web server in the address bar of the browser, and the browser sends a request to read the webpage.

(2) When the server receives the request, it generates a Set-cookie header, which is placed in the HTTP message to return the client together and initiates a session.

(3) After the client receives the answer, to continue the session, the contents of the Set-cook-ie are taken out and a Cookie.txt file is stored in the client computer.

(4) When the client makes a request to the server again, the browser first searches the computer for the Cookie.txt file of the Web site. If found, the cookie header is generated based on this Cookie.txt, which is sent to the server in the HTTP request message.

(5) The server receives a request containing a cookie header, retrieves information about the user in its cookie, and generates a page response from a client to be passed to the client. Every Web page request of a browser can pass an existing cookie file, for example, the browser's open or refresh page operation.

This is the basic principle of how cookies work.

In addition, there are two ways to set cookies in PHP:

1 invocation of the Setcookie function

Invocation of the 2session_start function

Here is a little explanation of the work done by Session_Start, Session_Start () once written, the server will generate a file with a filename called session_id on the server and send the Set-cookie header to the client at the same time. The content is the file name of the session stored on the phpsessid= server, that is, session_id, the client receives the response will generate a cookie file, that is Session_Start inside actually contains setcookie operation, It's just not as flexible as setcookie.

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.