2013-08-18 157 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