为什么我的cookies在iframe中不能正常工作?

时间:2011-02-01 00:46:38

标签: php cookies

这是我的原始登录页面

http://wordswithfriends.net/mytourney/login.php

测试用户如下: l:testuser p:test1234

此处显示用户名:

http://wordswithfriends.net/mytourney/test_cron.php

但是如果我将上面的iframe包装成如下:

http://wordswithfriends.net/?page_id=386

Cookie似乎不起作用。使用和不使用iframe显示此登录信息。使用iframe注销。

http://wordswithfriends.net/mytourney/test_cron.php

仍显示用户名。使用

注销

http://wordswithfriends.net/mytourney/index.php

这是完全相同的页面。现在用户名消失了。我怎样才能在两者之间保持一致?

3 个答案:

答案 0 :(得分:1)

您的iframe有www.wordswithfriends.net作为域名,您的页面没有。尝试使用通配符cookie *.wordswithfriends.net或匹配这两个域。

答案 1 :(得分:1)

有一个非常微妙的区别:

www.wordswithfriends.net (host: .wordswithfriends.net)

wordswithfriends.net (host: wordswithfriends.net)

由于主机域不同,您的Cookie会发生冲突。设置Cookie时,请使用setcookie()包含带有.domainname.net的域:

setcookie("cookiekey", $value, time()+3600, "/", ".wordswithfriends.net", 1);

(或者你这样做)

这将确保它适用于所有域,而不仅仅是wordswithfriends.net。

注意

您可能还想调整:

session_set_cookie_params(time()+3600, '/', ".wordswithfriends.net", true)

如果你的会话cookie也需要调整。 session_set_cookie_params()

答案 2 :(得分:0)

我认为this hack会对你有所帮助,因为它似乎是第三方的Cookie问题 - 但它有点怪异......