(我可能在这个答案中切换了域,但理论应该是一样的。)
您最好的选择是从 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);
}
然后,在框架容器上,您可以每隔一段时间检查一下用户是否已登录(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 提供示例的技能,无论如何,可能有很多。