[英]Migrate cookie domain from no domain to .mydomain.com with PHP/Nginx
I have a web site wrote with PHP and running on Nginx HTTP server. 我有一个用PHP编写并在Nginx HTTP服务器上运行的网站。 I have a cookie called "locale" which represents the user locale.
我有一个名为“ locale”的cookie,它代表用户的语言环境。 For 6 months, I am sending the cookie like this:
6个月来,我将像这样发送cookie:
setCookie("locale", "fr", time() + 36000, "/");
As a cookie less domain, I understand it's only available on my single domain "www.mydomain.com". 作为不含Cookie的域名,我知道它仅在我的单个域名“ www.mydomain.com”上可用。
But now, I want to create 2 news sub domains: 但是现在,我想创建2个新闻子域:
I see "locale" cookie set on www is not accessible by m or secure. 我看到m或安全无法访问在www上设置的“语言环境” cookie。 So now I am sending the cookie like this:
所以现在我要像这样发送cookie:
setCookie("locale", "fr", time() + 36000, "/", ".mydomain.com");
Yeah my cookie is available everywhere now! 是的,我的cookie现在随处可见! But as I have some users who had already browsed my web site, they have the domain less cookie + the new one, their browser send me both cookies but PHP gives me the former one.
但是,由于有些用户已经浏览过我的网站,因此他们的域名较少,而cookie却是新域名,他们的浏览器向我发送了两个cookie,但PHP给了我前一个。 As a result nobody can change the locale any more.
结果,没有人可以再更改语言环境。
Question is: how can I migrate from this domain less cookie to a domain specified cookie? 问题是:如何从该域较少的cookie迁移到域指定的cookie? Without change the cookie name.
不更改cookie名称。
The first cookie you set without the domain defaulted to www.mydomain.com and because that's more specific than '.mydomain.com' it's choosing the original cookie. 您设置的第一个不带域的Cookie默认为www.mydomain.com,因为它比“ .mydomain.com”更具体,所以选择的是原始Cookie。 Read the accepted answer here.
在此处阅读已接受的答案。
As conclusion, I will put this in my index.php file during 1 week: 作为结论,我将在1周的时间内将其放入我的index.php文件中:
setCookie('locale', null, time() - 5000, '/');
In order to delete the domain-less cookie. 为了删除无域cookie。 I have tested on all major browser, and I don't see any conflict with:
我已经在所有主流浏览器上进行了测试,但看不到与以下任何冲突:
setCookie('locale', 'fr', time() + 36000, '/', '.mydomain.com');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.