[英]How to implement openid google, yahoo and others
我正在嘗試實現OpenID,為此我下載了http://www.openidenabled.com/php-openid,並從中選擇了Auth文件夾,將任何內容更改為localhost目錄,並創建了index.php文件,其代碼為如下:
<?php
if (!isset($_POST['submit'])) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>OPENID TESTING</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Sign in with your OpenID: <br/>
<input type="text" name="id" size="30" />
<br />
<input type="submit" name="submit" value="Log In" />
</form>
</body>
</html>
<?php
} else {
// check for form input
if (trim($_POST['id'] == '')) {
die("ERROR: Please enter a valid OpenID.");
}
// include files
require_once "Auth/OpenID/Consumer.php";
require_once "Auth/OpenID/FileStore.php";
// start session (needed for YADIS)
session_start();
// create file storage area for OpenID data
$store = new Auth_OpenID_FileStore('./oid_store');
// create OpenID consumer
$consumer = new Auth_OpenID_Consumer($store);
// begin sign-in process
// create an authentication request to the OpenID provider
$auth = $consumer->begin($_POST['id']);
if (!$auth) {
die("ERROR: Please enter a valid OpenID.");
}
// redirect to OpenID provider for authentication
$url = $auth->redirectURL('http://localhost/', 'http://localhost/oid_return.php');
header('Location: ' . $url);
}
?>
現在,當我嘗試填寫來自worldpress或myopenid.com的ID時,就可以識別我並將我轉移到提供者頁面進行身份驗證,但是在Google,yahoo或其他服務提供者的情況下不會發生這種情況。 我必須為Google或Yahoo實現的功能
我正在使用PHP LightOpenID庫( 請參閱gitorious上的內容 ),以使用戶能夠使用其Google帳戶登錄我的網站。 對於其他OpenID提供程序,您只需更改$openid->identity
字段。 它為我們處理所有身份驗證流程。 您無需擔心令牌和內容。
在此頁面中,我顯示“使用Google登錄”鏈接:
<?php
require_once 'openid.php';
$openid = new LightOpenID;
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
$openid->returnUrl = 'http://my-website.com/landing-login.php'
?>
<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>
當單擊鏈接時,將出現一個Google頁面,要求他進行身份驗證和/或授權您檢索他的電子郵件。
然后,他將被重定向到登錄頁面$openid->returnUrl
。 該頁面的代碼應為:
<?php
require_once 'openid.php';
$openid = new LightOpenID;
if ($openid->mode) {
if ($openid->mode == 'cancel') {
// User has canceled authentication
} elseif($openid->validate()) {
// Yeah !
$data = $openid->getAttributes();
$email = $data['contact/email'];
} else {
// The user has not logged in via Google
}
} else {
// The user does not come from the link of the first page
}
?>
如果要從用戶檢索更多信息,則必須將它們添加到第一頁中的$openid->required
中。 例如 :
$openid->required = array(
'contact/email',
'namePerson/first',
'namePerson/last'
);
如果用戶接受的話,也會讓您在第二頁中獲得自己的名字和姓氏:
$name = $data['namePerson/first'] . " " . $data['namePerson/last'];
希望能有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.