2012-04-09 89 views
1

这里设置的Cookie是网页中的问题: http://www.customazon.com/demo从不同域-SRC的iframe

主页(@ customazon.com)加载包含辅助域的iframe(@ gamekeg.com)。我想让用户使用提供的密码登录到管理控制面板。问题是,由于它是第二个域名,浏览器将其视为“第三方Cookie”,并且绝大多数都会拒绝它们。我需要找到一种方法来允许在此iframe中设置cookie。让用户调整自己的cookie设置是一个选项。

事情我已经尝试:

  1. 设置在头一个P3P短版(许多不同版本的CP =字符串): 标题('P3P:CP =“IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONI HIS我们的IND CNT“');

  2. 创建(可能是不正确的,但我已经尽我可以管理)一个P3P长版本policy.p3p和p3p.xml文件。

  3. 一些奇怪的JavaScript加载一个隐藏的iframe和张贴到它(Safari的解决方法吗?)。

没有丝毫的工作。任何可以给予帮助,找到一种方法来允许这将是伟大的。

回答

3

(我可能得到的域在这个答案调换了,但理论上应该是一样的。)

你最好的选择是做从gamekeg.com登录页面cross-domain AJAX request到customazon.com (您需要发送一些特殊的标题以允许跨域请求 - 请阅读该链接)。在正常情况下,除非您控制两个站点(您似乎),否则这是不可能的。

在gamekeg.com登录页面后,用户已成功登录,就可以作出这样一个电话:

// I don't expect you to use jQuery, but I don't recall the entire 
// AJAX process off of the top of my head. You may have to set 
// xhr.withCredentials = true or something. 
$.ajax(
    "http://customazon.com/ajax_login.php", 
    { 
     "username": <?php echo $username; ?>, 
     "password_hash": <?php echo $password_hash; ?> 
    } 
); 

ajax_login.php可能是这样的:

// Send appropriate cross-domain headers here. 
// In addition, you must configure your crossdomain.xml in your root. 
header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); 
header("Access-Control-Allow-Credentials: true"); 
header("Access-Control-Allow-Origin: http://source.com"); 
header("Access-Control-Allow-Headers: Content-Type, *"); 
if (isset($_POST["username"]) && isset($_POST["password_hash"])) { 
    setcookie("username", $_POST["username"], time() + 24 * 60 * 60); 
    setcookie("password", $_POST["password_hash"], time() + 24 * 60 * 60); 
} 

然后,在框架容器上,您可以经常检查以查看用户是否已登录(readCookie取自QuirksMode):

function readCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0;i < ca.length;i++) { 
      var c = ca[i]; 
      while (c.charAt(0)==' ') c = c.substring(1,c.length); 
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

function checkAjaxLogin() { 
    if (readCookie("username") !== null && readCookie("password")) { 
     // You're logged in now; refreshing the page should 
     // do the rest, assuming the cookies are named correctly. 
     window.location.refresh(); 
    } 
} 

如果你可以使用Flash,但是,该进程可能加快为Flash请求并不关心跨域策略。但是,我没有Flash技术来提供示例,反正可能有很多。

+1

我申请你的建议,所有除了checkAjaxLogin,只是因为我不能肯定它做什么,或者为什么它是必要的。 ajax似乎正确发布。 ajax_login似乎正在发挥其作用。 crossdomain.xml就在那里。但是,我仍然没有看到任何cookie被设置。 – 2012-04-09 08:02:50

+0

如果您现在尝试登录,你会在屏幕顶部的var_dump($ _ COOKIE)(从gamekeg.com)看到。登录后,您会看到来自运行在customazon上的ajax的响应alert()。它显示数据已收到,setcookie被调用,并且还打印出$ _COOKIE(来自customazon.com)。这两个cookie var_dumps都显示空数组。我现在想念什么? – 2012-04-09 08:05:25

+0

我实际上没有看到任何框架。他们应该在哪里? – 2012-04-09 19:20:48