Home  >  Article  >  Backend Development  >  PHP cannot obtain cookie problem processing

PHP cannot obtain cookie problem processing

卡哇伊
卡哇伊Original
2020-07-06 10:11:512996browse

How to deal with the problem that php cannot obtain cookies: first set a cookie with the key a and the value value; then search for the cookie of a in the cookie string and return its value; finally refresh the browser and visit again When it comes to the server, there is one more a=value.

PHP cannot obtain cookie problem processing

Problem details

First write the following simple code:

The code is as follows:

<?php 
    setcookie(&#39;a&#39;,&#39;value&#39;);
    print $_COOKIE[&#39;a&#39;];

When accessing for the first time, an error is reported:

The reason for the error is that the value of $_COOKIE['a'] does not exist. Second visit: value

Related learning recommendations: <a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">php video tutorial</a><br>

Question: Why is there no cookie when I visit for the first time? ? Shouldn't I set it first and then get it? ?

Answer: Use firebug of firefox to view "Network":

Client:

As you can see, the browser (client) sends a request to the server. When making the request, various parameters are included in the request header information to tell the server what kind of text I want to receive (Accept), What encoding format (Accept-Encoding), what language (Accept-Language), etc., of course, the cookie is also passed to the server (Cookie).

Server side:

First step: setcookie('a','value')

Because cookie is Set on the client side, the setcookie function itself cannot set cookies. It can only tell the browser through header information: Brother, I want to set a cookie with the key a and the value value. You can help me set it up at your place. . You can also understand it as: "Come, I am happy today and I will give you a cookie."

Second step:$_COOKIE['a']$_COOKIE['a']

It's very simple. The operation is to bring the cookie from the browser Search the string for the cookie with key a and return its value.

Obviously, this cookie with "key a" cannot be found, because when the client accesses the server, this cookie does not exist at all, and the previous step The header information of the cookie has not been returned to the client yet (php will not return to the client until the statement is executed from top to bottom)

Step 3: Server returns information

Among them, the returned header information contains Set-Cookie a=value. The browser receives this header information and stores the cookie in a file on the computer. The storage location of the cookie seems to be similar for different browsers. Different, this is outside the scope of this article.

When you refresh the browser and access the server again, a lot of header information will also be brought to the server, but this time there will be more cookies. Aa=value. So $_COOKIE['a'] can naturally find the value of the cookie with the key a from the cookie string.

The above is the detailed content of PHP cannot obtain cookie problem processing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn