Cookie被设置两次 [英] Cookies being set twice
问题描述
为什么为我的网站设置了两次cookie?一组cookie用于 www.example.com,而同一组存储在 example.com。
Why are cookies being set twice for my website? One set of cookies is stored for "www.example.com" and the same set is being stored for "example.com".
推荐答案
最有可能的是,您允许同时通过域名 example.com
和 www.example.com $ c $来访问您的网站c>并且您尚未指定应为其设置Cookie的域名。
setcookie()
第五个参数> 指定域,因此仅将其设置为 example.com
。这也将使其对更高域的 www.example.com
可用。
Most likely, you are allowing access to your site via both domain names example.com
and www.example.com
and you have not specified the domain name for which your cookie should be set. The fifth parameter to setcookie()
specifies domain, so set it to example.com
only. That will make it available to the higher domain www.example.com
as well.
setcookie('key', 'value', time(), '/path', 'example.com');
通过URL重写,您可以在设置Cookie之前将您网站的所有用户强制转到这些域中的一个或另一个,但这只是解决方案,以确保用户体验的一致性,而不是管理cookie的方法。
Via URL rewriting, you can force all users of your site to one or the other of those domains before the cookie is set, but that is merely a solution for consistency of user experience, not a way to manage cookies.
从文档中:
域
cookie可用的域。将域设置为 www.example.com将使Cookie在www子域和更高子域中可用。较低域名(例如 example.com)可用的Cookie将可访问较高子域名(例如 www.example.com)。仍实施不推荐使用的»RFC 2109的旧版浏览器可能需要一个领先版本。匹配所有子域。
domain
The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.
这篇关于Cookie被设置两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!