Home  >  Article  >  Backend Development  >  How to solve the problem that php cookie cannot get the value

How to solve the problem that php cookie cannot get the value

藏色散人
藏色散人Original
2021-05-12 09:22:112083browse

php cookie cannot get the value solution: 1. Set "setcookie('a','value')"; 2. Execute "$_COOKIE['a']$_COOKIE['a'] "; 3. Just ask the server to return information.

How to solve the problem that php cookie cannot get the value

The operating environment of this article: windows7 system, php5.5.12 version, DELL G3 computer

php cannot obtain cookie problem for the first time

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 The value of $_COOKIE['a'] does not exist. Second visit:

#Q: Why is there no cookie during the first visit? ? Shouldn't I set it first and then get it? ?

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

Client:

You can see that the browser (client ) makes a request to the server. When making the request, various parameters are included in the request header information, telling the server what kind of text (Accept), what encoding format (Accept-Encoding), and what language I want to receive ( Accept-Language), etc., of course, the Cookie is also passed to the server (Cookie).

Server side:

Step one: setcookie('a','value')

Because the cookie is set on the client, the setcookie function itself cannot set the cookie , it can only tell the browser through the header information: Brother, I want to set a cookie, the key is a, the value is 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."

The second step: $_COOKIE['a']$_COOKIE['a']

is very simple. The operation is to search for the key in the cookie string brought by the browser. a's cookie and returns 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 different for different browsers. This is not the case. 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 in the cookies brought, There is one more a=value. So $_COOKIE['a'] can naturally find the value of the cookie with the key a from the cookie string.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem that php cookie cannot get the value. 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