Domain name and cookie (domain name suffix) _ PHP Tutorial

Source: Internet
Author: User
Tags set cookie in domain
Domain name and cookie problems (domain name suffix ). The domain name and cookie come up with the following question: can www.g.cn set the cookie to .g.cn? can www.com.cn set the cookie to .com.cn? Test results: No. Because the browser knows Domain name and cookie
By accident, I think that www.g.cn can set the cookie to .g.cn. can www.com.cn set the cookie to .com.cn?

Test results: No. Because the browser knows that the suffix of www.com.cn is .com.cn rather than. cn, it is prohibited to set cookies.
Because the browser has a built-in domain name suffix list. Todo: if a new suffix appears in the future, but the old browser cannot update the list, wouldn't it allow cookie settings?

Extension suffix Primary domain name Second-level domain name
Www.g.cn . Cn G.cn * .G.cn
Www.com.cn .Com.cn Www.com.cn * .Www.com.cn
Www.google.com.cn .Com.cn Google.com.cn * .Google.com.cn

Can www.example.com read the .example.com cookie?
Yes.
Can www.example.com read example.com cookies?
No. Todo: use www.example.com and example.com as SSO to prevent the cookie from being sent to static.example.com.
Can example.com read the cookie of www.example.com?
A: No.
Setcookie ('A', 'A', time () + 1234, '/', 'example. com '); is the cookie set to .example.com or example.com?
A: It is .example.com.
If you want to set example.com cookies, you need to use setcookie ('default', 'default', time () + 1234 ,'/');.
Cookie setting and reading range:

HTTP request domain name Primary domain name Cookie configurable (and readable) range Cookie cannot be set The cookie cannot be read.
Example.com Example.com Example.com, .example.com Www.example.com Www.example.com
Www.example.com Example.com Www.example.com, .www.example.com, .example.com Example.com Example.com
G.com.cn G.com.cn G.com.cn, .g.com.cn .Com.cn
Www.com.cn Www.com.cn Www.com.cn, .www.com.cn .Com.cn

Set cookie code:

The code is as follows:


Setcookie ('default', 'default', time () + 1234 ,'/');
Setcookie ('A', 'A', time () + 1234, '/', 'example. com ');
Setcookie ('B', 'BB ', time () + 1234,'/',' .example.com ');
?>


Read cookie code:

The code is as follows:


Var_dump ($ _ COOKIE );
?>


Result:

Then I accidentally thought of a question: can www.g.cn set the cookie to .g.cn? can www.com.cn set the cookie to .com.cn? Test results: No. Because the browser knows that...

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.