When I think of a problem: www.g.cn can set the cookie to .g.cn, can www.com.cn set the cookie to .com.cn?
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: