跨子域名和主机的Cookie

跨子域名和主机的Cookie

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

问题描述

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



使用下面的PHP代码在 c> if

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



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



只要以下任何信息相关:

- www.mydomain.com 是IIS 6.0

- distant.mydomain.com 是Apache 2.2.9

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

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



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

解决方案

质疑原始帖子中包含的代码和信息是完全正确的,可以正常工作。



问题是当您引入其他技术时。例如,我已经知道,通过Python模块发送PHP代码,一个允许Django提供PHP文件/内容的代码改变了很多关于脚本可访问的内容,什么不是。



根据 Marc Novakowski ,谁建议发送 $ _ COOKIE 到日志,以了解有什么。



我也检查 $ _ SERVER 和 $ _ GET 。这是空虚的 $ _ GET ,这使我失望了,我试图使用的设置并不像我以前那么简单。这是一个错误的理解,导致在原始帖子中没有包含Django的信息。



感谢所有回复的人问题!


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.

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');

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

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

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

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.

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.

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.

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.

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.

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.

Apologies and thanks to all who responded to this question!

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

08-21 11:02