Google supports the OpenID 2.0 protocol, providing authentication support as an OpenID provider. On request from a third-party site, Google authenticates users who are signing in with an existing Google account, and returns to the third-party site an identifier that the site can use to recognize the user. This identifier is consistent, enabling the third-party site to recognize the user across multiple sessions. Google also supports the following extensions:

Google Federated Login allows users to leave your web site, sign in to their Google accounts, and return to your web site with a unique identifier (sort of like a username). The Google API also allows you to recieve the user’s email address upon their return.

Here is a simplified version of how the process works:

  • The user clicks “Login with Google” on your web site
  • Your web site makes an API call to Google and fetches a redirect URL
  • Your web site redirects the user to the given URL
  • Google authenticates the user
  • Google redirects the user back to your web site and passes the user’s identifier (and, optionally, their email address)

Database
Sample database users table columns id, email, oauth_uid, oauth_provider and

 

 CREATE TABLE users ( id INT PRIMARY KEY AUTO_INCREMENT, email VARCHAR(70), oauth_uid int(11), oauth_provider VARCHAR(100), username VARCHAR(100) );

In this tutorial we have the following directory structure

google-open //Google LightOpenID library
config
— functions.php
— dbconfig.php //Database connection
index.php
home.php
getGoogleData.php //The Callback URL file
login-google.php

dbconfig.php
Database configuration file.

 

< ?php

define('DB_SERVER', 'dbserver');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');

define('USERS_TABLE_NAME', 'users_table_name'); //Replace your users table name here
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

login-google.php
In root directory find out the below line at login-google.php code and replaceyourdomain.com with your own domain  value.

define('CALLBACK_URL', 'http://yourdomain.com/getGoogleData.php');

index.php
If you want to modify your web project existing login or index pages, just use following code.

 

< ?php
session_start();
if (isset($_SESSION['id'])) {
// Redirect to home page as we are already logged in
header("location: home.php");
}
if (array_key_exists("login", $_GET)) 
{
$oauth_provider = $_GET['oauth_provider'];
if ($oauth_provider == 'google')
{
header("Location: login-google.php");
}
}
?>
//HTML Code
Google Login

home.php
In home page you can display user details by accessing session variables.

 

Name: < ?php $_SESSIONS['username'] >
Email: < ?php $_SESSIONS['email'] >
Your are logged in with: < ?php $_SESSIONS['oauth_provider'] >
Logout from  < ?php $_SESSIONS['oauth_provider'] >

Please post your comments for any queries !