2013-08-18 156 views
0

我有一個示例應用程序(說app1)與用戶名和密碼字段..當用戶提交的價值,證書需要驗證與另一個網站登錄頁面(如app2),如果app2登錄那麼我將重定向到app1的主頁...PHP跨應用程序登錄驗證

是否可以通過PHP?

+0

我想你應該執行類似的OAuth http://oauth.net/ –

回答

1

當然是。您需要對包含數據的site2執行curl請求。然後檢查結果。如果登錄操作成功,您可以重定向您的用戶。

//You have recived username and password in post method, now it is time to log user in site2 
$c = curl_init(); 
curl_setopt($c, CURLOPT_URL, 'http://example.com/login.php'); 
curl_setopt($c, CURLOPT_POST, 1); 
curl_setopt($c, CURLOPT_POSTFIELDS, 'login='.$_POST['login'].'&password='.$_POST['password']); 
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($c, CURLOPT_COOKIEFILE, 'full path cookie.txt'); 
curl_setopt($c, CURLOPT_COOKIEJAR, 'full path cookie.txt'); 
$page = curl_exec($c); 
curl_close($c); 

//If he typed in wrong password, on site2 should be some error. 
$error_text = 'Incorrect login or password'; //Text that will show on site after unsuccessful login attempt 

//Check if is there any error on site2 
if (strpos($page, $error_text) !== false) { 
    header('Location: http://www.example.com/'); //Everything seems to be ok, when do you want to redirect user? 
} else { 
    echo 'You typed in incorrect username or password';  
} 
+0

你好Helid,我是新來的PHP,你的解釋看起來不錯..但可以請你進一步深入一點..並給我還有更多的註釋代碼行!謝謝 – fishspy

+0

當然可以。我在我的答案中添加了更多代碼。 –

+0

現在好酷!你能解釋這一行嗎? curl_setopt($ c,CURLOPT_POSTFIELDS,'login ='。$ _ POST ['login']。'&password ='。$ _ POST ['password']); – fishspy