PHP different subdomains of the same name cookie problem solving method _php Tutorial

Source: Internet
Author: User
Tags rfc setcookie subdomain
$_cookie is a very useful thing in PHP, but sometimes we will encounter the same domain name under the same sub-domain name, so there will be only one cookie to keep the problem, the following small part to introduce to you students.

PHP's hyper-global variable $_cookie brings a lot of convenience, and in some cases it can be confusing. For example, the root domain and sub-domains exist under the same name Cookie,$_cookie can only save one, which should be?

The RFC recommends using the longest-length one, with the highest precision, but different browsers handle different ways. I only tested the Chrome,chrome root domain and subdomain of the same name of the cookie is sent out, so that PHP only receive the same name in the preceding cookie, followed by ignored, so it is easy to receive the wrong value. It is said that Safari followed the RFC recommendations, did not test it personally, and no other browsers were tested.


First set the virtual domain name through switchhosts: www.bKjia.c0m, and configure the Web server, of course, you manually set up the Hosts file can also, I meant to introduce a few tools.

Then write the PHP script that sets the cookie, set the subdomain first, and then set the root domain:

The code is as follows Copy Code

Setcookie ("bar", "www", time () + ten, "/", "www.bKjia.c0m");
Setcookie ("Bar", "foo", Time () + ten, "/", ". bkjia.c0m");
?>

Then write a script that browses the cookie:

The code is as follows Copy Code

Var_dump ($_cookie);
?>

BTW: When I originally wrote the script when I actually used the var_dump before the Setcookie, that is, before sending the request header has the output, made such a beginner error is a sin, but even more surprising is that the script did not error, checked for a long time originally because the default output in PHP.ini _buffering = 4096.

First set up and then browse, you can see the results, the results show that the effective is a sub-domain cookie.

Re-open a browser window and use Webdeveloper to delete cookies or delete them manually to avoid any impact on the results.

Then swap the order of two calls to Setcookie, that is, set the root domain first, and then set the subdomain:

The code is as follows Copy Code

Setcookie ("Bar", "foo", Time () + ten, "/", ". bkjia.c0m");
Setcookie ("bar", "www", time () + ten, "/", "www.bKjia.c0m");
?>

First set up and then browse, you can see the results, the results show that the effective is the root domain of the cookie.

Repeat the test process two times and record the difference in the request header with Firebug:

First set up the subdomain, then set the root domain: The value of the request header cookie is bar=www;bar=foo, the result is bar=www
Set the root domain the second time, and then set the subdomain: The value of the request header cookie is bar=foo;bar=www, and the result is Bar=foo

Also said that the same name cookie for the server side of PHP, in the request header cookie, which in the first which in effect, the following will be ignored.

If you are not using Firefox, you will not be able to use Firebug, you can use PHP code to detect the cookie header:

The code is as follows Copy Code

if (Isset ($_server[' Http_cookie ')) var_dump ($_server[' Http_cookie ']);

The above experimental conclusion is based on Firefox, because different browsers send cookie policies may vary, so the results may be different on other browsers, such as in Safari is always a valid subdomain, other browsers such as Opera,chrome, etc. have not been carefully tested. In view of this chaotic conclusion, it is better not to use the same name cookie in the subdomain and root domain!

Conclusion: It is very unwise to use cookies with the same name in the root domain and subdomain.

http://www.bkjia.com/PHPjc/633196.html www.bkjia.com true http://www.bkjia.com/PHPjc/633196.html techarticle $_cookie is a very useful thing in PHP, but sometimes we will encounter the same domain name under the same sub-domain name, so there will be only one cookie to keep the problem, the following small series to give ...

  • Related Article

    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.