我目前正在实现并同时学习OpenId的使用。特别是使用lightopenid。它正在工作,但我希望需要登录的用户在访问 url www.example.com/login 时自动重定向到登录页面。下面的示例让用户单击一个按钮,然后将用户重定向到登录页面。如何使用 url www.example.com/login 自动将用户重定向到打开的 ID 登录页面?
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
# Change 'localhost' to your domain name.
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'https://www.google.com/accounts/o8/id';
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Google</button>
</form>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
只需在您的 singin 页面中使用它即可
<?php // index.php
require_once 'openid.php';
$openid = new LightOpenID("www.example.com");
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->returnUrl = 'http://www.example.com/login';
header('Location: ' . $openid->authUrl());
?>