Domain name and cookie
I accidentally thought of a question: www.g.cn can set the cookie to .g.cn, so can www.com.cn set the cookie to .com.cn?
Test result: No. Because the browser knows that the suffix of www.com.cn is .com.cn instead of .cn, it prohibits setting cookies.
Because the browser has a built-in domain name suffix list. todo: If a new suffix appears in the future and the old browser cannot update the list, will it allow cookies to be set?
|
extension后缀 |
一级域名 |
二级域名 |
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 cookie of .example.com?
Yes.
Can www.example.com read the cookie of example.com?
No. todo: Use SSO for www.example.com and example.com to prevent cookies from being brought to static.example.com.
Can example.com read the cookie of www.example.com?
Answer: No.
setcookie('a', 'aa', time() + 1234, '/', 'example.com'); Is the cookie set to .example.com or example.com?
Answer: It’s from .example.com.
If you want to set a cookie for example.com, you need to use setcookie('default', 'default', time() + 1234, '/');.
Cookie setting and reading range:
HTTP请求域名 |
一级域名 |
cookie可设置(并可读取)的范围 |
cookie不可设置 |
cookie不可读取 |
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:
Copy code The code is as follows:
< ?php
setcookie('default', 'default', time() + 1234, '/');
setcookie('a', 'aa', time() + 1234, '/', ' example.com');
setcookie('b', 'bb', time() + 1234, '/', '.example.com');
?>
Read cookie code:
Copy code The code is as follows:
var_dump($_COOKIE);
?>
Result screenshot:
http://www.bkjia.com/PHPjc/326043.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326043.htmlTechArticleDomain name and cookie I accidentally thought of a question: www.g.cn can set the cookie to .g.cn, then Can www.com.cn set the cookie to .com.cn? Test result: No. Because the browser knows...