这是有问题的页面:
http://www.customazon.com/demo
主页(@ customazon.com)会加载包含辅助域(@ gamekeg.com)的iframe。我想允许用户使用提供的密码登录到管理控制面板。问题在于,由于它是第二个域,因此浏览器将其视为"第三方Cookie",而大多数浏览器会完全拒绝它们。我需要找到一种方法来允许在此iframe中设置cookie。要求用户调整其Cookie设置不是一种选择。
我尝试过的事情:
在标头中设置P3P短版本(CP =字符串的许多不同版本):
header('P3P:CP =" IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
使用policy.p3p和p3p.xml文件创建(可能不正确,但是我已尽力做到了)一个P3P长期版本。
一些奇怪的javascript加载隐藏的iframe并将其发布(Safari解决方法?)。
丝毫没有任何作用。可以找到任何允许这样做的方法的帮助将是很大的。
(我可能已经在此答案中切换了域,但理论应相同。)
最好的选择是从gamekeg.com登录页面向customazon.com进行跨域AJAX请求(您需要发送一些特殊的标头以允许跨域请求-在该链接上了解更多信息)。 在正常情况下,除非您同时控制两个站点(您似乎希望如此),否则这是不可能的。
用户成功登录后,在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);
}
然后,您可以经常在框架容器上进行检查,以查看用户是否已登录(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方面的技能来提供示例,无论如何,可能还有很多。
我已经应用了您的建议,除了checkAjaxLogin之外,都只是因为我不太确定它的作用或必要性。 Ajax似乎正确发布。 ajax_login似乎正在发挥作用。 crossdomain.xml在那里。 但是,我仍然看不到设置任何cookie。
如果您现在尝试登录,则会在屏幕顶部看到var_dump($ _ COOKIE)(来自gamekeg.com)。 登录后,您将看到在customazon上运行的ajax的响应的alert()。 它显示已接收到数据,并调用了setcookie,还打印了$ _COOKIE(来自customazon.com)。 两个cookie var_dumps都显示空数组。 我现在想念什么?
我实际上没有看到任何帧。 他们应该在哪里?