(我可能得到的域在这个答案调换了,但理论上应该是一样的。)
你最好的选择是做从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技术来提供示例,反正可能有很多。
我申请你的建议,所有除了checkAjaxLogin,只是因为我不能肯定它做什么,或者为什么它是必要的。 ajax似乎正确发布。 ajax_login似乎正在发挥其作用。 crossdomain.xml就在那里。但是,我仍然没有看到任何cookie被设置。 – 2012-04-09 08:02:50
如果您现在尝试登录,你会在屏幕顶部的var_dump($ _ COOKIE)(从gamekeg.com)看到。登录后,您会看到来自运行在customazon上的ajax的响应alert()。它显示数据已收到,setcookie被调用,并且还打印出$ _COOKIE(来自customazon.com)。这两个cookie var_dumps都显示空数组。我现在想念什么? – 2012-04-09 08:05:25
我实际上没有看到任何框架。他们应该在哪里? – 2012-04-09 19:20:48