(我可能得到的域在這個答案調換了,但理論上應該是一樣的。)
你最好的選擇是做從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