2013-02-20 79 views
10

我想使用OpenID實現使用Google帳戶登錄,但我不知道如何啓動此過程,因爲我不知道如何執行此操作。那麼是否有任何一步一步的指導,以便我可以輕鬆地在PHP中使用CodeIgniter實現Google帳戶登錄。使用OpenID在CodeIgniter中用Google帳戶登錄

我只找到this,但我無法正確理解,因此有任何指南或任何圖書館可以使用Google帳戶登錄?

+1

請注意,自2015年4月以來,Google以OpenID登錄並取消與OpenID Connect的登錄,這是一個完全不同的後繼者,另請參閱:https://developers.google.com/identity/protocols/OpenID2Migration – 2015-09-27 18:29:05

回答

6

下載LightOpenID。創建login.php並粘貼以下代碼

<?php 

require_once 'openid.php'; 
$openid = new LightOpenID("my-domain.com"); 

if ($openid->mode) { 
    if ($openid->mode == 'cancel') { 
     echo "User has canceled authentication !"; 
    } elseif ($openid->validate()) { 
     $data = $openid->getAttributes(); 
     $email = $data['contact/email']; 
     $first = $data['namePerson/first']; 
     echo "Identity : $openid->identity <br>"; 
     echo "Email : $email <br>"; 
     echo "First name : $first"; 
    } else { 
     echo "The user has not logged in"; 
    } 
} else { 
    echo "Go to index page to log in."; 
} 

創建index.php頁並粘貼以下代碼

<?php 
require_once 'openid.php'; 
$openid = new LightOpenID("my-domain.com"); 

$openid->identity = 'https://www.google.com/accounts/o8/id'; 
$openid->required = array(
    'namePerson/first', 
    'namePerson/last', 
    'contact/email', 
); 
$openid->returnUrl = 'http://my-domain.com/login.php' 
?> 

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a> 

這是所有你做。代碼摘自Google Login with LightOpenID

+0

謝謝,但我獲取數據,如電子郵件和用戶名,但沒有得到每一次只有電子郵件和語言我用戶的所有參數像定義在openid.php文件,但沒有得到任何其他參數 – 2013-02-20 10:33:53

+0

下載登錄openid不工作。你可以給任何其他的網址。 – 2016-06-07 07:10:08

2

首先下載openid.php並放入codeigniter根文件夾。

1拷貝的代碼,並保存爲.... /控制器/ logingoogle.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class LoginGoogle extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('login_model'); 
    } 

    public function index() 
    { 
     require_once 'openid.php'; 
     $openid = new LightOpenID("localhost"); 
     $openid->identity = 'https://www.google.com/accounts/o8/id'; 
     $openid->required = array(
      'namePerson/first', 
      'namePerson/last', 
      'contact/email', 
      'birthDate', 
      'person/gender', 
      'contact/postalCode/home', 
      'contact/country/home', 
      'pref/language', 
      'pref/timezone', 
     ); 
// $openid->returnUrl = 'http://localhost/login_thirdparty/login_google.php'; 

    $openid->returnUrl = 'http://localhost/login_thirdparty/codeigniterlogin/index.php/logingoogle/loginAuth'; 

// echo '<a href="'.$openid->authUrl().'">Login with Google</a>'; 

     $data['openid'] = $openid; 
     $this->load->view('googleLoginView', $data); 
    } 

    public function loginAuth() 
    { 
     $this->login_model->index(); 
    } 
} 

2.拷貝的代碼,並保存爲.... /視圖/ googleLoginView.php

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Login using google account</title> 
</head> 
<body> 
    <a href = "<?php echo $openid->authUrl(); ?>" > Loging Using google account </a> 
</body> 
</html> 

3.複製代碼,並保存爲... /模型/ login_model.php

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
require 'openid.php'; 

class Login_model extends CI_Model 
{ 
    public function index() 
    { 
     $openid = new LightOpenID("localhost"); 

     if($openid->mode) 
     { 
      if($openid->mode == 'cancel') 
      { 
       echo "User has canceled authentication !"; 
      } 
      elseif($openid->validate()) 
      { 
       $data = $openid->getAttributes(); 
       $email = $data['contact/email']; 
       $first = $data['namePerson/first']; 
    //   header("Location: http://speechwithmilo.com/speechtherapy/adminpanel/"); 
       echo "Identity : $openid->identity <br />"; 
       echo "Email : $email <br />"; 
       echo "First name : $first"; 
       echo "<pre>"; print_r($data); echo "</pre>"; 

//    echo "<meta http-equiv = 'refresh' content = '0; url=http://speechwithmilo.com/speechtherapy/adminpanel/'>"; 
      } 
      else 
      { 
       echo "The user has not logged in"; 
      } 
     } 
     else 
     { 
      echo "Go to the login page to logged in"; 
     } 
    } 
} 
+0

我用你的解決方案,但遇到了一個錯誤。請在這裏看看我的問題。 http://stackoverflow.com/questions/22173781/login-through-google-in-codeigniter-via-openid – 2014-03-04 13:52:45

相關問題