子域和主机上的Cookie [英] Cookies across subdomains and hosts

查看:130
本文介绍了子域和主机上的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序中,我使用开发环境和语言的组合,我需要访问来自两个不同的子域,每个在不同的主机上的cookie。

In the application I'm writing using a combination of development environments and languages, I have need of accessing a cookie from two different subdomains, each on a separate host.

使用下面的PHP代码在 www.mydomain.com 上设置cookie,我试图从遥控访问它。 mydomain.com 在单独的主机上。

The cookie is being set on www.mydomain.com using the PHP code that follows, and I'm attempting to access it from distant.mydomain.com on a separate host.

setcookie('token', base64_encode(serialize($token)), time()+10800, '/', '.mydomain.com');  

我尝试访问 distant.mydomain.com 使用以下代码:

I'm trying to access the cookie from distant.mydomain.com using the following code:

if (isset($_COOKIE['token'])) { /* do stuff */ }  

问题: distant.mydomain .com 未找到Cookie 。如果刚才提到的语句返回false,即使cookie存在,

The problem: distant.mydomain.com is not finding the cookie. The if statement just mentioned returns false, even though the cookie exists.

我已经验证了设置的cookie是 mydomain.com (通过检查我的Firefox cookie)。我不能想到任何理由这不会工作。

I have verified that the cookie that is set is for mydomain.com (by checking my Firefox cookies). I can't think of any reason this wouldn't be working.

使用相同的 setcookie 代码,我有一个旧应用程序专门在 www .mydomain.com 主机,该应用程序能够访问跨域的cookie。这使我怀疑问题与单独的主机有关。

Using the same setcookie code, I have an old application running exclusively on the www.mydomain.com host, and that application is able to access the cookie across domains. This makes me suspect that the problem has to do with separate hosts.

只要以下任何信息是相关的:

- www.mydomain.com 是IIS 6.0

- distant.mydomain.com 是Apache 2.2.9

- 两个服务器都使用PHP 5.2.x

- 两个服务器都在Windows Server 2003上运行

Just in case any of the following information is pertinent:
- www.mydomain.com is IIS 6.0
- distant.mydomain.com is Apache 2.2.9
- Both servers use PHP 5.2.x
- Both servers are operating on Windows Server 2003

如果有任何我可以提供更详细的信息,以便更好地描述问题,请让我知道!

If there is any further information I can provide in order to better describe the problem, please let me know!

推荐答案

问题包含在原始帖子的代码和信息是完全正确的和工作正常。

For the benefit of anyone reading this question the code and information contained in the original post are exactly correct and work fine.

问题是当你介绍其他技术。例如,我知道,通过Python模块发送PHP代码,允许Django提供PHP文件/内容,改变了很多关于脚本可以访问的内容,而不是什么。

The problem is when you introduce other technology. For instance, I have since learned that sending PHP code through a Python module, one that allows Django to serve PHP files/content, changes a great deal about what is accessible to the script and what is not.

这最终是根据 Marc Novakowski ,他建议将 $ _ COOKIE 发送到日志,以了解该日志。

This was eventually discovered following the advice of Marc Novakowski, who suggested sending $_COOKIE to the log in order to find out what was there.

out $ _ SERVER $ _ GET 。这是空虚的 $ _ GET ,这让我想到,我试图使用的设置不像我想象的那么直接。这是错误的理解导致不包括关于Django的信息在原始的帖子。

I also checked out $_SERVER and $_GET. It was the emptiness of $_GET that tipped me off that the setup I am attempting to use is not as straightforward as I had thought. It was that mistaken understanding that led to not including the information about Django in the original post.

歉意问题!

这篇关于子域和主机上的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆