我想使用 OpenID 使用 Google 帐户实现登录,但我不知道如何开始这个过程,因为我不知道如何去做。那么是否有任何分步指南,以便我可以使用 PHP 中的 CodeIgniter 轻松实现 Google 帐户登录。
我只找到了这个,但我无法正确理解它,所以有任何指南或任何库可以使用 Google 帐户登录吗?
我想使用 OpenID 使用 Google 帐户实现登录,但我不知道如何开始这个过程,因为我不知道如何去做。那么是否有任何分步指南,以便我可以使用 PHP 中的 CodeIgniter 轻松实现 Google 帐户登录。
我只找到了这个,但我无法正确理解它,所以有任何指南或任何库可以使用 Google 帐户登录吗?
下载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。
首先下载openid.php并放入您的 codeigniter 根文件夹。
1.复制代码并保存为..../controller/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.复制代码并保存为..../views/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.复制代码并保存为..../models/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";
}
}
}