PHP:Cookie域/子域控制 [英] PHP: Cookie domain / subdomain control

查看:177
本文介绍了PHP:Cookie域/子域控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上使用多个子网域,其中一些网站应该有自己的会话。



我想我已经做好了,注意到一些关于cookie处理,我不明白。我在文档中没有看到解释它的任何东西,所以想想我会看看这里的任何人是否有一些光在这个问题上。



如果我只是:

  session_start(); 

我最终得到了一个会话cookie,如下所示:



subdomain.example.net



但是,如果我尝试自行设置Cookie域,例如

  ini_set('session.cookie_domain','subdomain.example.net'); 

或类似

 code> session_set_cookie_params(0,/,subdomain.example.net,false,false); 

我最后得到一个.subdomain.example.net的cookie我相信是指匹配所有子域(或在这种情况下子子域)。



这似乎发生在所有我的cookie实际上,而不只是会话。如果我没有设置域,那么它只需使用当前域就可以了。



任何想法是什么原因导致的,以及我可以做什么来控制该前缀点?



谢谢!

  header( Set-Cookie:cookiename = cookievalue; expires = Tue,06-Jan-2009 23:39:49 GMT; path = /; domain = subdomain.example.net); 


I'm working on a site with multiple subdomains, some of which should get their own session.

I think I've got it worked out, but have noticed something about cookie handling that I don't understand. I don't see anything in the docs that explains it, so thought I would see if anyone here has some light to shed on the question.

If I just do:

session_start();

I end up with a session cookie like this:

subdomain.example.net

However, if I make any attempt to set the cookie domain myself, either like

ini_set('session.cookie_domain', 'subdomain.example.net');

or like

session_set_cookie_params( 0, "/", "subdomain.example.net", false, false);

I end up with a cookie for .subdomain.example.net (note the opening dot), which I believe means "match all subdomains (or in this case sub-subdomains).

This seems to happen with all my cookies actually, not just session. If I set the cookie domain myself, it automatically has the dot prepended, meaning this domain and all subs of it. If I don't set the domain, then it gets it right by using only the current domain.

Any idea what causes this, and what I can do to control that prepending dot?

Thanks!

解决方案

PHP's cookie functions automatically prefix the $domain with a dot. If you don't want this behavior you could use the header function. For example:

header("Set-Cookie: cookiename=cookievalue; expires=Tue, 06-Jan-2009 23:39:49 GMT; path=/; domain=subdomain.example.net");

这篇关于PHP:Cookie域/子域控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆