How to get the domain of a specific cookie?

13,204

Solution 1

I don't think the domain is available when reading cookies, this is limited by the browser. A solution would be to remove the old cookie and change it to the new domain.

E.g.

$value = $_COOKIE['TestCookie'];
setcookie("TestCookie", "", time() - 3600, "www.example.com");
setcookie("TestCookie", $value, time + (60 * 60 * 24 * 30), ".example.com");

Solution 2

You can use PHP function session_get_cookie_params(). I hope this will make the job.

domain:"www.domain_name.com"
httponly:false
lifetime:1525181833
path:"/"
secure:false

Solution 3

If I understand you correctly you want to change the domain of the cookies currently existing on the clients?

This is not possible(*).

When getting a cookie server side, is it possible for you to see if it was set for the www domain, keeping in mind that the cookie passed form the client has no domain information?

(*) It might be possible with JavaScript on the client side.

Share:
13,204
lvil
Author by

lvil

r

Updated on June 04, 2022

Comments

  • lvil
    lvil almost 2 years

    There is a web site www.example.com
    All cookies are set to the www subdomain.
    Now there is a new subdomain and I want the cookies to be seen for all subdomains.

    The goal is to rewrite the www.example.com cookies for all the old visitors to be .example.com or to write new ones for .example.com if set for www.

    For this I want to get the domain of the existing cookies.
    Is it possible? Is there a php function for this purpose?