0

我有一个带有用户名和密码字段的示例应用程序(比如 app1)。当用户提交值时,需要使用另一个网站登录页面(比如 app2)验证凭据,如果 app2 登录成功,那么我将重定向到主页app1的页面...

可以通过PHP吗?

4

1 回答 1

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';    
}
于 2013-08-18T14:23:21.647 回答