如何在2个子域上共享Cookie? [英] How to share cookie on 2 sub domains?
问题描述
当我的移动网站在子域上运行时,我想在两个域之间共享一个Cookie.
I would like to share a cookie across 2 domains as my mobile site runns on a subdomain.
生产服务器:
www.server.com
m.server.com
开发服务器:
rabbit.server
rabbit.m.server
我用来设置cookie的PHP代码如下:
My PHP-code to set the cookie looks like this:
if ($settings['development'] == true) // intranet does not work with subdomains :-(
setcookie($cookiename,$sessid, $expires,'/','',0);
else // production
setcookie($cookiename,$sessid, $expires,'/', $subdomain.'.'.$domain['name'],0);
我如何在两个域之间共享此cookie,以使客户端登录到两个站点?
How could I share this cookie across the 2 domains in order to have the client loged in on both sites?
推荐答案
嗯,有两种方法可以做到这一点.
Well, there's two ways of doing this.
您可以在整个域上设置cookie,这将允许您从任何子域访问它,或者,如果您只希望允许某些子域,则必须创建两个cookie,每个cookie.
You can either set the cookie on the whole domain, which will allow you to access it from any subdomain, or if you wish to only allow certain subdomains then you'll have to create two cookies, one for each.
对于两个不同的子域,您不能只有一个Cookie,仅 ;您可以在整个域中启用它;也可以有多个Cookie,每个cookie一个子域.
You can't have one single cookie for two different subdomains only, you can enable it on the whole domain, or you can have multiple cookies, one for each subdomain.
必须按代码进行更改
setcookie($cookiename,$sessid, $expires,'/', $subdomain.'.'.$domain['name'],0);
到
setcookie($cookiename,$sessid, $expires,'/','.'.$domain['name'],0);
这篇关于如何在2个子域上共享Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!