简体   繁体   English

当当前子域具有相同名称的cookie时,如何访问在根域上设置的cookie?

[英]How to access cookie set on root domain when the current subdomain has a cookie with the same name?

I have two cookies with the same name set - one on my root domain, and one on a subdomain. 我有两个具有相同名称集的cookie-一个在我的根域上,一个在子域上。 By default, if you are on a matching subdomain, PHP will read the subdomain cookie and ignore the root cookie. 默认情况下,如果您在匹配的子域中,则PHP将读取子域cookie,并忽略根cookie。 How would I read the root domain cookie when there is a matching subdomain cookie? 当存在匹配的子域cookie时,我将如何读取根域cookie?

For example, lets say I have these two cookies: 例如,假设我有以下两个cookie:

Name | Value | Domain
uid  | 12345 | .example.com
uid  | abcde | app.example.com

If I am on app.example.com, PHP will only show me this in the $_COOKIE global array: 如果我在app.example.com上,PHP只会在$_COOKIE全局数组中向我显示此内容:

array("uid" => "abcde");

However, I want to be able to access this: 但是,我希望能够访问此:

array("uid" => "12345");

Is this possible? 这可能吗?

I've tried using session_set_cookie_params like this: 我已经尝试过使用session_set_cookie_params这样的:

session_set_cookie_params(0, "/", ".example.com");
session_start();
echo "<pre>"; print_r($_COOKIE["uid"]); echo "</pre>";

But it still returns: 但是它仍然返回:

array("uid" => "abcde");

EDIT: 编辑:

The cookies are being set with the following code: 使用以下代码设置Cookie:

setcookie("uid", "12345", 0, "/", "example.com");
setcookie("uid", "abcde", 0, "/", "app.example.com");

如果您在可访问其他子域的根域中创建cookie,并且没有将此cookie用于该子域,则建议您重命名该子域的cookie。

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

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