2011-12-16 50 views
2

感謝stackoverflow我得到了我的小項目,但我需要一些建議了。我想檢查用戶是否登錄了domain1.com並在domain2上返回消息。代碼還有很多。下面我列舉了一個基本的例子。在http://domain2.com檢查用戶是否從不同的域登錄

<script type="text/javascript" src="http://domain1.com/test.php?js"></script> 

test.php的

的index.php是http://domain1.com

<?php 
if (isset($_GET['js'])){ 
header("Content-type:text/javascript"); 
?> 

function doAjax(){ 
$.getJSON("http://domain1.com/index.php/home/callback.php?name=name&callback=?", 
function(message) { 
alert("Data Saved"); 
}); 
} 

document.write('<button onclick="doAjax();">Submit</button>'); 
<?php } ?> 

<?php exit; } ?> 

callback.php在http://domain1.com。這是我想要檢查用戶是否登錄的位置。如果用戶登錄,文件被寫入,如果沒有,我想發送消息到domain2.com要求登錄。

<?php 
$callback = $_GET['callback']; 

$name = $_GET['name']; 

$myFile = "txt/tester.txt"; 
$fh = fopen($myFile, 'a') or die("can't open file"); 
fwrite($fh, $name); 
fclose($fh); 

header("Content-Type: application/javascript"); 
?> 

<?php echo $callback; ?>("Message from the server"); 

這最後一部分我從上一個問題得到。 <?php echo $callback; ?>("Message from the server");如果這是domain2的消息,我該如何調用它?

回答

0

因此,基本上,域2期望從域1上的callback.php中獲得JSONP對象。要在JSON對象中設置消息的格式,請將您的消息包含在關聯數組中(例如:$msg = array('message' => 'This is the callback message');,然後將其傳回給域2 echo $_GET['callback'].'('.json_encode($msg).')';此外,在您的header()聲明答案設置Content-Typeapplication/json

+0

感謝。你能告訴我一切順利?我一直很努力,但不能讓它開始工作。 – Ciprian 2011-12-17 16:55:32