你的问题太不具体,无法给出准确的答案。如果您尝试让用户使用 Google 帐户登录您的网站,则会有记录here http://code.google.com/apis/accounts/docs/GettingStarted.html.
另一方面,如果您尝试让用户使用一个帐户登录您控制的多个网站,则可以按以下方法操作:
使站点上的所有登录链接都指向集中登录页面,但在链接中包含有关用户来源的信息。例如:
<a href='http://login.example.com/login.php?source=my.other.site.com/foo/bar'>log in!!</a>
然后,一旦用户成功登录,您就可以将用户重定向回原始站点,同时传递有关经过身份验证的用户所需的任何信息。
但是,您还需要确保人们无法通过向 URL 添加必要的身份验证参数来绕过您的身份验证机制。这可以通过包含参数的 HMAC-SHA-256 形式的签名以及存储在登录服务器和原始站点上的秘密来完成。 (对于使用 SSO 系统的每个站点,此密钥最好应该不同。)
<?php
$MySecretKey = 'Nobody Will Ever Guess This!!';
// Generate signature from authentication info + secret key
$sig = hash(
'sha256',
$user->id . $user->email,
$MySecretKey
);
// Make sure we're redirecting somewhere safe
$source = parse_url($_GET['source']);
if(in_array($source->host, $list_of_safe_hosts))
$target = 'http://'.$source->host.$source->path;
// Send the authenticated user back to the originating site
header('Location: '.$target.'?'.
'user_id='.$user->id.
'&user_email='.urlencode($user->email).
'&sig='.$sig);
?>
然后,在原始站点中,如果签名匹配,则用户已经登录。将有关登录用户的信息存储在会话变量(不是 cookie)中:
<?php
$MySecretKey = 'Nobody Will Ever Guess This!!';
// Set not logged in by default
$user_id = 0;
$user_email = '';
if(intval($_GET['user_id']) && !$_SESSION['user_id']) // Someone trying to log in?
{
// See if they have the right signature
if (hash_equals(hash('sha256', intval($_GET['user_id']).$_GET['user_email'], $MySecretKey), $sig)) {
$_SESSION['user_id'] = intval($_GET['user_id']);
$_SESSION['user_email'] = $_GET['user_email'];
}
}
?>
请注意,我使用的是 PHP 5.6 中添加的函数:hash_equals http://php.net/manual/en/function.hash-equals.php。如果你的版本低于 5.6,你可以使用这个替代函数来实现时序安全比较 http://blog.ircmaxell.com/2014/11/its-all-about-time.html函数使用双重HMAC验证 https://www.isecpartners.com/blog/2011/february/double-hmac-verification.aspx:
function hash_equals($a, $b) {
$key = mcrypt_create_iv(128, MCRYPT_DEV_URANDOM);
return hash_hmac('sha512', $a, $key) === hash_hmac('sha512', $b, $key);
}
这显然是一个非常粗糙的实现,但它应该是一个不错的起点。