Cookie path and cookie domain
In the previous blog post "Using cookies to achieve skinning function" , I wrote about the usage of cookies. In this article, I mainly explain the path and domain in cookies.
cookie path
Cookies are generally created because users visit a page, but this cookie is not accessible only on the page where the cookie was created . By default, for security reasons, only web pages in the same directory as the page that created the cookie or in a subdirectory of the page that created the cookie can be accessed . Then if you want its parent or the entire web page to be able to use cookies at this time , you need to set the path.
To make this set cookie accessible to other directories or parent directories:
document . cookie = "userName= Lone Ice Sea ; path=/" ;
Cookie 域
The path can solve the problem of accessing cookies under the same domain , so how to solve the access problem under the same main domain? We can do this by specifying the hostname that can access the cookie .
document.cookie="name=value;domain=cookieDomain";
For example , "www.baidu.com" and "mp3.baidu.com" share an associated domain name "baidu.com" , if we want the cookies under "www.baidu.com" to be accessed by "mp3.baidu.com" , we need to use the domain attribute of the cookie , and we need to set the path attribute to "/"
One thing to note here: it must be access between the same domains, and the value of domain cannot be set to a domain name other than the main domain.
document . cookie = "username= Lonely Ice Sea ; path=/; domain=baidu.com"
Welcome to communicate