How to get the domain of a specific cookie?

六眼飞鱼酱① 提交于 2019-12-10 13:13:18

问题


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?


回答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");



回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/7913452/how-to-get-the-domain-of-a-specific-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!