OpenID Login with google and PHP

Example implementation how to use google OpenID for a login to a Website.
http://woppr.de/openid/google.php

Prerequisite:
LightOpenID PHP 5 library for openid authentication (openid.php)

Google, completely ignores optional parameters, and for the required ones, it supports, according to it’s website:

  • namePerson/first (first name)
  • namePerson/last (last name)
  • contact/country/home
  • contact/email
  • pref/language

Code:

 
<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
    $openid = new LightOpenID('woppr.de');
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'https://www.google.com/accounts/o8/id';
 
            $openid->required = array('namePerson/first', 'namePerson/last', 'contact/email');
 
            header('Location: ' . $openid->authUrl());
        }
?>
<form action="?login" method="post">
    <button>Login with Google</button>
</form>
<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        if($openid->validate()) {
		$returnVariables = $openid->getAttributes();
		echo 'User ' . $openid->identity . ' has logged in with this email address ' . $returnVariables['contact/email'] . ' Name: ' . $returnVariables['namePerson/first'] . ' ' . $returnVariables['namePerson/last'];
		} else {
		echo 'User has not logged in.';
		}
 
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert