简体   繁体   English

使用OpenID在CodeIgniter中使用Google帐户登录

[英]Login with Google account in CodeIgniter with OpenID

I want to implement a login with a Google account using OpenID, but I have no idea how to start this procedure because I have no idea about how to do it. 我想使用OpenID使用Google帐户实现登录,但我不知道如何启动此过程,因为我不知道如何执行此操作。 So is there any step by step guides for this so that I can easily implement a Google account login with CodeIgniter in PHP. 那么是否有任何分步指南,以便我可以使用PHP中的CodeIgniter轻松实现Google帐户登录。

I've found only this but I cant understand it properly so is there any guides or any libraries to login with a Google account? 我发现只有这个,但我无法理解它,所以有任何指南或任何图书馆登录谷歌帐户?

Download LightOpenID . 下载LightOpenID Create the login.php file, and paste the following code into the file. 创建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.";
}

Create the index.php file, and paste the following code into the file. 创建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>

This is all you need to do. 这就是你需要做的。 The code has been taken from Google Login with LightOpenID . 该代码来自使用LightOpenID的Google登录

At first download openid.php and put in your codeigniter root folder. 首先下载openid.php并输入你的codeigniter根文件夹。

1. copy the code and save as ..../controller/logingoogle.php 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. copy the code and save as ..../views/googleLoginView.php 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. copy the code and save as ..../models/login_model.php 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";
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM