简体   繁体   English

使用PHP / Nginx将cookie域从无域迁移到.mydomain.com

[英]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个新闻子域:

  1. m.mydomain.com (the mobile part) m.mydomain.com(移动部分)
  2. secure.mydomain.com (a secure version) secure.mydomain.com(安全版本)

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. 在此处阅读已接受的答案。

How to get the domain of a specific cookie? 如何获取特定Cookie的域?

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.

相关问题 PHP邮件未从mydomain.com发送到Google应用程序电子邮件myname@mydomain.com - PHP mail is not sending from mydomain.com to the google apps email myname@mydomain.com .href.replace给我双重域名(http:// localhosthttp://mydomain.com) - .href.replace giving me double domain (http://localhosthttp://mydomain.com) 网址重写:将mydomain.com/index.php更改为mydomain.com/home - URL rewriting: Change mydomain.com/index.php to mydomain.com/home Laravel 4.1使用301永久重定向将所有请求从www.mydomain.com重定向到mydomain.com - Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect 在Heroku上将app.mydomain.com作为mydomain.com/app运行 - Run app.mydomain.com as mydomain.com/app on Heroku Symfony 为 mydomain.com/parameter 创建路由 - Symfony create routing for mydomain.com/parameter 使用PHP设置“ mydomain.com/username”的任何简单,整洁的方法吗? - Any easy, noncluttering way to set up “mydomain.com/username” using PHP? 用PHP动态cookie域 - dynamic cookie domain with php 带有域参数的PHP cookie - php cookie with domain parameter Nginx配置以node为domain.com和php在子文件夹中运行为domain.com/phptest - nginx configuration to run node as domain.com and php in subfolder as domain.com/phptest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM