Home > Article > Backend Development > Solution to the problem of cookies with the same name in different subdomains of PHP_PHP Tutorial
$_COOKIE is a very useful thing in php, but sometimes we encounter different subdomain names under the same domain name, so there will be a problem of only keeping one cookie. The editor will introduce it to you below. one time.
PHP’s superglobal variable $_COOKIE brings a lot of convenience, but can also cause confusion in some cases. For example, there are cookies with the same name in the root domain and subdomains, and only one can be saved in $_COOKIE. Which one should it be?
RFC recommends using the one with the longest length, which has the highest accuracy, but different browsers handle it differently. I only tested Chrome. In Chrome, cookies with the same name in the root domain and subdomains are sent out. In this way, PHP only receives the cookies with the same name in the front, and the later ones are ignored. This makes it easy to receive wrong values. Safari is said to have followed the RFC's recommendations and has not been tested personally, nor by other browsers.
First, set the virtual domain name through SwitchHosts: www.bKjia.c0m, and configure the Web server. Of course, you can also manually set the Hosts file. My intention is to introduce a few more tools.
Then write a PHP script to set cookies, first set the subdomain, and then set the root domain:
The code is as follows | Copy code | ||||||||||||||||||||||||||||
Write the script to browse cookies:
Set it first and then browse, and you can see the results. The results show that the cookies under the subdomain are valid. Reopen a browser window and use WebDeveloper to delete cookies, or delete them manually to avoid affecting the results. Then change the order of calling setcookie twice, that is, set the root domain first, and then set the subdomain:
The above experimental conclusions are based on Firefox. Since different browsers may have different strategies for sending cookies, the results may be different on other browsers. For example, under Safari, the subdomain is always valid. , other browsers such as Opera, Chrome, etc. have not been carefully tested. In view of this confusing conclusion, it is better not to use cookies with the same name in subdomains and root domains! Conclusion: It is currently very unwise to use cookies with the same name in root domains and subdomains 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 Previous article:The Chinese file name of the file uploaded by php is garbled_PHP tutorialNext article:The Chinese file name of the file uploaded by php is garbled_PHP tutorial Related articlesSee more |