简体   繁体   English

从子域到域的setcookie

[英]setcookie from subdomain to domain

I have: 我有:

     mydomain.com (which is the portal of the game, global setting and stuff)
game.mydomain.com (which is the the actual game)

The problem is that I want to set a cookie that is available globally, on game.mydomain.net , mydomain.net (and whatever subdomain i'm going to create in the future). 问题是我想在game.mydomain.netmydomain.net (以及将来我要创建的game.mydomain.net域)上设置一个全局可用的cookie。

I've been trying to set the cookie from another subdomain as I've read that subdomains can set cookies to parent domains but not vice versa (which is wierd and I guess I've read it wrong). 我一直在尝试从另一个子域设置cookie,因为我已经知道子域可以将cookie设置为父域,但反之则不行(这很奇怪,我想我读错了)。 Whatever, so I've done another account.mydomain.com (from which I'm making an ajax call form mydomain.net so the user can authenticate) and I'm using 无论如何,所以我完成了另一个account.mydomain.com(我正在从该帐户进行mydomain.net的ajax调用,以便用户可以进行身份​​验证)并且我正在使用

setcookie('session', $value, time() + 2592000 (one month), '/', '.tribul.net');

Then, return the success message and refresh the main page on mydomain.net so it can read the new cookie value.. problem is, there's no cookie set. 然后,返回成功消息并刷新mydomain.net上的主页,以便它可以读取新的cookie值。问题是,没有设置cookie。 I've also been trying to set the cookie from mydomain.com (as .tribul.net) so it can be avaialable on all subdomains but it's available only on the main domain. 我也一直试图从mydomain.com(作为.tribul.net)设置cookie,以便它可以在所有子域上使用,但仅在主域上可用。 What's wrong? 怎么了?

I need to connect all subdomains and the domain to the same cookie, TO BE NOTICED, I am setting the cookie in a backend file named process.php (placed in account.domain.com) as result of an ajax request. 我需要将所有子域和域连接到同一个cookie,要注意,由于ajax请求,我将cookie设置在名为process.php(放置在account.domain.com中)的后端文件中。

试试这个setcookie('session', $value, time() + 2592000 , '', '.tribul.net');

In php.ini: 在php.ini中:

session.cookie_path = /

session.cookie_domain = ".mydomain.com"

Set Cookie: 设置Cookie:

setcookie('session', $value, time() + 2592000, '/', 'mydomain.com');

为了使用我的问题,我使用了Klaus Hartl的jquery cookie插件 ,因为我无法从ajax后端设置全局cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM