Matt Koskela
Matt Koskela

Reputation: 5319

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. By default, if you are on a matching subdomain, PHP will read the subdomain cookie and ignore the root cookie. How would I read the root domain cookie when there is a matching subdomain cookie?

For example, lets say I have these two cookies:

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:

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(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:

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

Upvotes: 0

Views: 2755

Answers (1)

Aaronidas
Aaronidas

Reputation: 158

if you create a cookie in the root domain accessible for other subdomains and you do not use this cookie for this subdomain, I recommend you to rename the subdomain's cookie.

Upvotes: 1

Related Questions