9lessons programming blog
Loading Search
9lessons Bring Friends
Wall Script
Follow Me:
Monday, July 18, 2011

Login with Google Account OpenID

Few months back we had posted an article about login with Twitter and Facebook, it is most popular on 9lessons.info. We developed a system to quick login with Google account using OpenID authentication. This script helps you to avoid registration forms and email verification system, It’s is very quick, useful and simple to integrate.

Login with Google open id


Download Script     Live Demo

Developer
Ravi Tamada
Ravi Tamada
Designer Developer & Freelance
Chennai, INDIA
androidhive.info

Database
Sample database users table columns id, email, oauth_uid, oauth_provider and username.
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 replace yourdomain.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
<a href="?login&oauth_provider=google">Google Login</a>

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'] >
<a href="logout.php?logout">Logout</a> from  <?php $_SESSIONS['oauth_provider'] >

If any queries please comment here.
Comments
{ 125 comments }
Anonymous said...

nice and excellent tut, but in demo some redirect errors..

Miraaz ul Faiz said...

So sweet :)

Flavio said...

nice man.

testinou32 said...

Nice tuto !

Anonymous said...

can we log out this time? =)

Bernard said...

Thanks..

Eddy said...

cool

Anonymous said...

Thanks Ravi :)

Ali BEN MESSAOUD said...

يا سلعة تفتق على ع الأخر

extraplay social network said...

oauth_uid doesn't post to the users table? value is '0'?

soscod said...

nice tuto...everything make easy now....

thanks

Anonymous said...

You are the man...Thanks!

Anonymous said...

cooooool

Raghavendra Nayak said...

Its awesme Ravi :)

Kamran said...

I click on Download script & Demo link & got below error
http://analytics.hosting24.com/high_cpu.htmlsubscribe/index_google.php

Not Found

The requested URL /high_cpu.htmlsubscribe/index_google.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Jocuri said...

Hey, it's easy to login with Google. Thanks for tut. it is great.

Social media news said...

Great help.. thanks.

Anonymous said...

awesome

wanna.to said...

Thanks! Looks easier that FB login.

NormInfotech said...

very nice bt how to download

Anonymous said...

i am not able to get the script :(

Please help

Thanks

Indian seo company said...

Thanks for sharing the post. Nice tutorial. Would like to try it.

Anonymous said...

hi its really bice

Anonymous said...

is the oauth_uid supposed to be 0?

Anonymous said...

very nice bt how to download

Please help

pabx panasonic said...

very nice tutorial, i like. thanks.

Srinivas Tamada said...

@Kamran

Server CPU overload please try after sometime.

Anonymous said...

when i use it on localhost then line no 86 of openid.php gives error in curl and wrapper() functions.

Anonymous said...

When click on google login then it takes too much execution time and i got following error

Fatal error: Maximum execution time

Zern said...

Fatal error: Uncaught exception 'ErrorException' with message 'You must have either https wrappers or curl enabled.' in C:\AppServ\www\login\google\google-open\openid.php:86 Stack trace: #0 C:\AppServ\www\login\google\login-google.php(14): LightOpenID->__construct() #1 C:\AppServ\www\login\google\login-google.php(10): gooleAuthenticate() #2 {main} thrown in C:\AppServ\www\login\google\google-open\openid.php on line 86


I got this . Can you help me to solve the problem ?

raja gulshair said...

cool

zakaria said...

I get this error

Notice: Undefined variable: uid in D:\www\google-openid\getGoogleData.php on line 11

sekhar said...

Me too getting the same error as Zern. author! please respond..

Anonymous said...

i am trying to download it ..
but it told me that my email no subscribed
although i subscribed it
plz find to me way to download this
as i need it asap
srry for my poor english
thnx in advance

Anonymous said...

Kurwa w chuj to nie działa !

Anonymous said...

nice

Skin Care Reviews said...

Wow so nice! I like this site.Keep it working every day

Anonymous said...

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /mnt/154/sdb/5/8/whereisstef/google-open/openid.php on line 49

Ary Wibowo said...

thanks for the script. it works!!!

ashique said...

after clicking sign in button in gmail i got an FORBIDDEN error in my server. how to solve it. any help will be apriciated.

SupMusic said...

It only works for @gmail.com accounts, not for other Google Accounts..

Rock said...

Hi there! this is one of the most useful script for me at the time..thnk u!!

I need a little favour can we login with gmail account without going on the gmail login page.

Plzz suggest me!! i'll waiting for ur feedback..
tahnk u!

BioBuster said...

Is it really secure?
I can login on example page with your credentials.
http://demos.9lessons.info/google-openid/getGoogleData.php?openid.ext1.value.firstname=Ravi&openid.ext1.value.email=ravi8x%40gmail.com&openid.ext1.value.lastname=Tamada

sahas said...

Duniyaa me wo karo jo tumhe shi lage

Anonymous said...

oauth_uid doesn't post to the users table?
isn't there a oauth id at google ?

thanks !

Mathan raj said...

It stores only my username and email address.. why??????????

Anonymous said...

How to get country and language?

Unknown said...

Here, using this Google API you can get only the three vales name and email info. you can't get gender , profile photo and other info you can't get here. for that you have to go to Google plus API.

IME said...

Thanks. Try your best! :)

Anonymous said...

Java sample code is available?

Anonymous said...

hi srinivas, when i logged-out,then press login again,why it shows that i've logged in,whereas i login not yet in login

Anonymous said...

i got forbidden error on my server while redirecting to getGoogleData.php

Anonymous said...

fantastic

seri said...

Do not know about it. Google really surprised me again, as well as information on your blog))

Julian said...

Do you know if this process also supports login in with a Google Apps account?

Anonymous said...

Hello Srinivas

i have a problem in 'oauth_uid' What is this for as no value is entered when the user logs in? Please Help, I think may have this problem.

Anonymous said...

Awesome. Good work & a Great post

chuch said...

Thank you for tutorial
Now just get only email.

I would like to ask question : How to get contact detail (country, city, phone, street,...)

rajeshkumar said...

Hi

I really like you posts

recently i am subscribed in your site but i got the conformation mail and clicked that but when i am trying to download the script still asking the confomation

Please help me on this

Anonymous said...

how to dowload this?

amit said...

Live demo url is not working, please check it.

Anonymous said...

Always nice stuff here! love it! tx

Anonymous said...

Hi, srinivas
Veerendra here i implemented your login with facebook and twitter in my joomla project and its working fine thanks but i have a problem with login google as when it redirect back to my site after entering credentials at google account its gives me 403 forbidden error i tried giving 777 permission to the file but then it stop calling the file what to do please give suggetion hope you will revert

veerendra raghuvanshi
php developer
veerendra2406@gmail.com

Ankit J said...

I think almost all of us are using Google account so it is very convenient. Thanks for share.

longvelo said...

Good

nayanjyoti said...
This comment has been removed by the author.
nayanjyoti said...

How can we do this login in a popup and get the data in the parent window?

okankucuk said...

Nice tutorial as usual... i want to download but ...(New subscribers email data update every 18 hours... i have waintin more than a day, but it doesnt accept my email subscribe)

ramakant yadav said...

hi i got this errors when try to login with this
You don't have permission to access /int/sign_in.php on this server.

Anonymous said...

Hi Srinivas.
Instead of domain name i used the localhost . i am getting empty page . is localhost domain name work ?

Thanks advance ..

Anonymous said...

Good Tute, But if you cance the "No Thanks" Info, it does not redired in "Demo"

Anonymous said...

Hi Srinivas,
I download this script in my server. unzip that script. http://localhost/google-openid/login-google.php page displays empty . Please help me .

Thanks

Anonymous said...

Thanks , thanks a lot , it also working localhost also.

Thank you so much..

:-)

Anonymous said...

how to download its not working i had subcribed also till it saying plz subscribe...

Anonymous said...

Excellent, Excellent, Excellent ...

Pradeep said...

Excellent code... Work fine. Now i want to logout too. (Single logout). Please help...

PHP Web Designer said...

I am regular viewer of this site and have download so many script from this site. Thanks

Anonymous said...

hi thanks for great tutorial. After login to gmail, how can i get contact list. there is no function for this in functions.php file.

ATC website said...

thanks

Anonymous said...

This tutorial works well but I need to get the User Google ID in addition to the names and email.
I did not find anyway to do so. Do you have any clue ?
Thanks in advance,

André

vishal narkhede said...

awesome work dude....:-)

Abhishek said...

Nice tutorial but when I'm running it on localhost I'm unable to be redirected to home.php page.

Can u please elaborate more on callback URL. what actually it is.

FrankC said...

hi, great tutorial. although i get a 'Cannot modify header information - headers already sent by..'. It appears to work once I bypass your index.php page and run google_login.php directly. Is there a way around this ? thanks

Green Chapter said...

I get this and couldn't it:

"You don't have permission to access /phpinc/fbtlogin/getGoogleData.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

Anonymous said...

keep getting this

You don't have permission to access /phpinc/fbtlogin/getGoogleData.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

John David said...

$uid not valid , what it's mean ? Can you explain it for me ?

Suresh Gothandapani said...

The logout is not clearing the session values. I am still logged in after clicking logout... anything wrong with the script. It's a great script though.

Malkhan Parmar said...

Nice script...
Thank You....

abhishek rawat said...

i m facing the same issue as suresh mentioned earlier .. hope u solve this issue..

vinodhagan thangarajan said...

It's working nice....

Thank u ravi

Anonymous said...

got this error,
Error: invalid_request
Error in parsing the OpenID auth request.

can anyone help me?? and one more thing,

$userdata = $user->checkUserGoogle($uid, 'Google', $username, $email);


this line is in getGoogleData.php, where is the $uid comes from?

jitu said...

hi......... thanks for great tutorial it also simple for understand..........

Nimesh jain said...

what are the following files?
where to get these files?


getGoogleData.php //The Callback URL file
login-google.php

Anonymous said...

Hi! This is really great but I'm just wondering if this can be done using Ajax? If so, do you have a code for that one..? Thanks!

sanjay jaiswal said...

hiiiiiiiiiiiiiiiiii this is great plugin but url redirect problem ocarr.

Spyraa said...

Hey nice tutorial man... U make it so easy... Thanks buddy!!

Ramchandra poudel said...

Fatal error: Uncaught exception 'ErrorException' with message 'You must have either https wrappers or curl enabled.' in /data/multiserv/users/970671/projects/2220318/www/google-open/openid.php:86 Stack trace: #0 /data/multiserv/users/970671/projects/2220318/www/login-google.php(14): LightOpenID->__construct() #1 /data/multiserv/users/970671/projects/2220318/www/login-google.php(10): gooleAuthenticate() #2 {main} thrown in /data/multiserv/users/970671/projects/2220318/www/google-open/openid.php on line 86



what i do

subhash kumar said...

Hi dude,
This is really very nice stuff,but can somebody explain me what is the value of $uid which is assined to $_SESSION['oauth_id'] = $uid; in getGoogleData.php file.

Thanks in advance.

Kaliyamoorthy Moorthy said...

Hi,

Openid support HTTPS?

Because am getting "Error in parsing the OpenID auth request." Error.

Thanks in advance.

Unknown said...

php boss neengathan you know tamil sir i love u ur hard work

Unknown said...

ok thank you boss

Anonymous said...

Hi...,

How to restrict my users to login only with their Google apps account using above method?

Vivek said...

Is it possible in blogger blog

Anonymous said...

Thanks a lot ..superb job dude. simple and easy to undersatand

Divya said...

How much user details i can fetch from gmail login to my database, please help me...

Anonymous said...

Why am i getting this error ?
You don't have permission to access /google-o/getGoogleData.php on this server.

Hemanta Nandi said...

You don't have permission to access /google-o/getGoogleData.php on this server.

Fayiz Km said...

great

Gokulkrishh said...

good tutorial.

Anonymous said...

thank you.
everything is working fine except the logout.php.
even though i click "logout from google" i am still logged-in in the gmail.
the session destroy is not logging me out from the gmail. please help me someone. thank you.

Umar Farook said...

Just Awesome.Great!
Do we need to provide oauth key or any API key?

Umar Farook said...

I got this Error and also empty $uid is inserting to DB

Warning: Cannot modify header information - headers already sent by (output started at /home/coremind/public_html/dev/config/functions.php:16) in /home/coremind/public_html/dev/getGoogleData.php on line 20

Suresh khandelwal said...

very nice tutorial, i like.
Thanks for this article.

Virtual Geek said...

Nice cool, is very terrify!!

Dixit said...

Awesome Srinivas. Loved it. Very easily installed on my website and working fine. Can I use OpenID for logging to Yahoo and other accounts as well?

Sagar Patil said...

its nice tutorial dude

Sagar Patil said...

Nice tutorial thank you

Vishwa Kr Deepak (Vishu) said...

its work perfectly fine in local host , but when i upload it on bigrock server and change

$openid = new LightOpenID('localhost');
to
$openid = new LightOpenID('www.MyDomainName.com');
and
$openid->returnUrl = 'http://www.MyDomainName/xyz/login.php';

its showing error
Error: invalid_request
Error in parsing the OpenID auth request.

please suggest what necessary changes i need to do

Thanks

Anonymous said...

thanks

sam said...

NIce article but how to download the code ?

Steve Miller said...

Great feature!

Vikram Anand said...

You are GOD!! Bhai .. I have been breaking my head for last three days to make aPlugin which can access all three like google twitter and facebook.. twitter and facebook was no problem but google flow and structure I was making me Mad bcoz of URL redirection then I saw your tutorial which was based on OPEN ID and this is kick ass I would say ... thank you very much

Tan said...

how can I get it to ask for additional fields that user has to fill manually before it completes the registration?

Justin said...

Hi Srini,
I am trying to send html form in email and the user can respond to it.
In case I am sending the html form to a gmail distribution list, how can I capture the gmail id of the person who is submitting the html form in email?
Any suggestions or workarounds are welcome..

Ravi said...

Error: invalid_request
Error in parsing the OpenID auth request.

Post a Comment