2011-05-18 51 views
1

我想實現的OpenID和我下載http://www.openidenabled.com/php-openid並從我拿起驗證文件夾改變任何東西爲localhost目錄,並創建了一個index.php文件,其代碼爲如下圖所示:如何實現OpenID的谷歌,雅虎和其他

<?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填寫ID或myopenid.com被認可和幫我轉接到供應商頁面進行認證,但是這是不是與谷歌,雅虎或其他服務提供商的情況下發生的。我必須爲谷歌或雅虎實施

回答

3

我使用PHP LightOpenID庫(see on gitorious)使用戶能夠使用他們的Google帳戶登錄我的網站。對於其他OpenID提供商,您只需更改$openid->identity字段。它爲我們處理所有的認證流程。你不需要打擾令牌和東西。

這裏,我顯示鏈接「與谷歌登錄」頁面:

<?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> 

當鏈路上的點擊,谷歌的頁面會出現要求他進行認證和/或授權你找回他的電子郵件。

然後他將重定向到登陸頁面$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']; 

希望幫助!

+0

我試着添加'namePerson/first','namePerson/last'似乎無法與谷歌或雅虎(我收到的電子郵件和其他值)工作。任何想法爲什麼? – 2013-08-08 16:18:53

相關問題