PHP setcookie() cannot store value for the first time

不言
Release: 2023-03-24 13:36:01
Original
1892 people have browsed it

First write the following simple code:

Copy code The code is as follows:

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

First visit When, an error is reported:

The reason for the error is that 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 beyond the scope of this article.

Refresh the browser and access the server again. A lot of header information will also be sent to the server, but this time there is an extra a=value in the cookie. So $ _COOKIE['a'] can naturally find the value of the cookie with the key a from the cookie string.

Related recommendations:

Login verification example code using php cookies

php Cookies operation class

The above is the detailed content of PHP setcookie() cannot store value for the first time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!