简体   繁体   English

PHP:Cookie域/子域控件

[英]PHP: Cookie domain / subdomain control

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. 我想我已经解决了这个问题,但是我注意到了一些我不理解的cookie处理方法。 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: 我最终得到了一个像这样的会话cookie:

subdomain.example.net subdomain.example.net

However, if I make any attempt to set the cookie domain myself, either like 但是,如果我尝试自己设置cookie域,或者喜欢

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). 我最终得到了.subdomain.example.net的cookie(注意开头的点),我相信这意味着“匹配所有子域(或者在这种情况下是子子域)”。

This seems to happen with all my cookies actually, not just session. 这实际上发生在我所有的cookie上,而不仅仅是会话。 If I set the cookie domain myself, it automatically has the dot prepended, meaning this domain and all subs of it. 如果我自己设置cookie域,它会自动添加点,这意味着该域及其所有子域。 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. PHP的cookie函数自动在$ domain前加一个点。 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");

If you run your PHP script under " http://subdomain.example.net ", don't use the domain parameter : 如果您在“ http://subdomain.example.net ”下运行PHP脚本, 请不要使用domain参数

setcookie('cookiename','cookievalue',time()+(3600*24),'/');

You will get a cookie with "subdomain.example.net" (and not ".subdomain.example.net") 你会得到一个带有“subdomain.example.net”的cookie(而不是“.subdomain.example.net”)

If you read all of RFC 6265, you'll realize that the only proper way to have a "host-only" cookie is to NOT set the domain attribute. 如果您阅读了所有RFC 6265,您将意识到拥有“仅限主机”cookie的唯一正确方法是不设置域属性。

http://tools.ietf.org/html/rfc6265#section-5.4 http://tools.ietf.org/html/rfc6265#section-5.4

I realise this is an old question but I was having this problem and none of the answers above quite did it. 我意识到这是一个老问题,但我遇到了这个问题,上面的答案都没有完成。

I wanted to set the session cookie for a subdomain, but also enable httponly and secure. 我想为子域设置会话cookie,但也启用httponly和secure。

To avoid a leading . 避免领先。 infront of the subdomain, Kevin and stolsvik are correct don't set the domain attribute. 面向子域,凯文和stolsvik是正确的,不设置域属性。

So to do this and still be able to set httponly and secure mode, set the domain to NULL as follows: 因此,要执行此操作并仍然能够设置httponly和安全模式,请将域设置为NULL,如下所示:

session_set_cookie_params(0, '/', NULL, TRUE, TRUE);

You will now have a session cookie, for a specific subdomain (without a leading .) with httponly and secure set to true. 现在,您将拥有一个会话cookie,用于特定的子域(没有前导。),其中httponly和secure设置为true。

This may help someone (i spent some hours to figure this out). 这可能对某人有帮助(我花了几个小时来弄清楚这一点)。 After make the changes in the source files and before you test it, close your browser to properly destroy PHPSESSIONID in all domains and subdomains. 在源文件中进行更改之后,在测试之前,请关闭浏览器以正确销毁所有域和子域中的PHPSESSIONID。

Hope this save some time! 希望这节省一些时间!

I was having a problem to set cookies on wordpress and this helped me, the domain value was the key to get it working in all the pages 我在wordpress上设置cookie有问题,这对我有帮助,域值是让它在所有页面中运行的关键

$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;

setcookie("cookie_name", 'cookie_value', 0, '/', $domain);

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

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