This post explains you about how to implement Google OpenID authentication in PHP.
In this post we are using Openid API for google authentication. After download this script change database connectivity in inc/dbConnect.inc.php file and change $callback_website_url variable in login_process.php file.
Sample database design for table name contact.
This table contains id (primary key),tutorial and link.
CREATE TABLE IF NOT EXISTS `google_login` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(500) NOT NULL, `email` varchar(500) NOT NULL, `inserted_on` datetime NOT NULL, PRIMARY KEY (`id`) )
dbConnect.inc.php
Contains database connectivity code
$mysql_db_hostname = "Host name"; $mysql_db_user = "UserName"; $mysql_db_password = "Password"; $mysql_db_database = "Database Name"; $con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password or die("Could not connect database"); //Create a new connection mysql_select_db($mysql_db_database, $con) or die("Could not select database"); // select database
index.php
if (isset($_SESSION['UNAME']) && !(empty($_SESSION['UNAME']))){ echo '<a href="logout.php"><b><u>Logout</u></b></a><br/><br/>'; echo "Name:-".$_SESSION['UNAME']."<br/><br/>"; echo "Email:-".$_SESSION['UEMAIL']; // After Successful Authentication }else{ echo '<a href="login_process.php">Login With Gmail</a>'; // login page link }
login_process.php
Contains the login part and include openid api file.
include('inc/lightopenid.php'); $callback_website_url='http://www.91weblessons.com/demo/GoogleOpenIDAuthenticationInPHP/google_landing.php'; // After Authentication, google will redirect page to $callback_website_url googleAuthenticate($callback_website_url); function googleAuthenticate($callback_website_url) { $openid = new lightopenid; $openid->identity = 'https://www.google.com/accounts/o8/id'; $openid->returnUrl = $callback_website_url; $endpoint = $openid->discover('https://www.google.com/accounts/o8/id'); $fields ='?openid.ns=' . urlencode('http://specs.openid.net/auth/2.0') . '&openid.return_to=' . urlencode($openid->returnUrl) . '&openid.claimed_id=' . urlencode('http://specs.openid.net/auth/2.0/identifier_select') . '&openid.identity=' . urlencode('http://specs.openid.net/auth/2.0/identifier_select') . '&openid.mode=' . urlencode('checkid_setup') . '&openid.ns.ax=' . urlencode('http://openid.net/srv/ax/1.0') . '&openid.ax.mode=' . urlencode('fetch_request') . '&openid.ax.required=' . urlencode('email,firstname,lastname') . '&openid.ax.type.firstname=' . urlencode('http://axschema.org/namePerson/first') . '&openid.ax.type.lastname=' . urlencode('http://axschema.org/namePerson/last') . '&openid.ax.type.email=' . urlencode('http://axschema.org/contact/email'); header('location:'.$endpoint . $fields); // Redirect to google site }
google_landing.php
After Authentication, google will redirect page
session_start(); include('inc/dbConnect.inc.php'); if (!empty($_GET['openid_ext1_value_firstname']) && !empty($_GET['openid_ext1_value_lastname']) && !empty($_GET['openid_ext1_value_email'])) { $username = $_GET['openid_ext1_value_firstname'] . $_GET['openid_ext1_value_lastname']; $email = $_GET['openid_ext1_value_email']; $uname=$_GET['openid_ext1_value_firstname'].' '.$_GET['openid_ext1_value_lastname']; $email=$_GET['openid_ext1_value_email']; $checkUserSql="select * from google_login where email= '$email'"; $checkUserRes=mysql_query($checkUserSql); $checkUserCount=mysql_num_rows($checkUserRes); // Check email id checking in database if($checkUserCount == 0){ $sql="insert into google_login(name,email,inserted_on) values('$uname','$email',now())"; mysql_query($sql); // If user email id not present in database than add it } $_SESSION['UNAME']=$_GET['openid_ext1_value_firstname'].' '.$_GET['openid_ext1_value_lastname']; $_SESSION['UEMAIL']=$_GET['openid_ext1_value_email']; } header('location:index.php');
Billy Eslava
I was looking for this thanks for the share.
Tracy Gorman
Hi, nice article. I really like it!
Billy Eslava
This is a really good read for me, thank you!
Abhi
Thanks for this, can you please write about outlook with php
Tom Nich
Yes, It’s working fine, And now waiting for facebook and yahoo login..