如何获得特定cookie的域?

时间:2022-08-23 10:11:51

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.

有一个网站www.example.com所有的cookie都设置为www子域。现在有一个新的子域,我希望所有子域都能看到cookie。

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.

我们的目标是将www.example.com的cookie重写为.example.com,如果设置为www,则为.example.com编写新的cookie。

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

为此,我希望获得现有cookie的域。是可能的吗?为此目的有php函数吗?

3 个解决方案

#1


3  

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.

我认为读取cookie时域不可用,这是受浏览器限制的。解决方案是删除旧的cookie并将其更改为新域。

E.g.

如。

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

#2


1  

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

如果我理解正确,您想要更改客户机上当前存在的cookie的域吗?

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?

在获取cookie服务器端时,您是否有可能看到它是否为www域设置,并记住从客户机传递的cookie没有域信息?

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

(*)在客户端JavaScript可能是可能的。

#3


0  

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

可以使用PHP函数session_get_cookie_params()。我希望这能成功。

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

#1


3  

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.

我认为读取cookie时域不可用,这是受浏览器限制的。解决方案是删除旧的cookie并将其更改为新域。

E.g.

如。

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

#2


1  

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

如果我理解正确,您想要更改客户机上当前存在的cookie的域吗?

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?

在获取cookie服务器端时,您是否有可能看到它是否为www域设置,并记住从客户机传递的cookie没有域信息?

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

(*)在客户端JavaScript可能是可能的。

#3


0  

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

可以使用PHP函数session_get_cookie_params()。我希望这能成功。

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