PHP Login Or Sign In With Google Account By OpenID
Posted by in PHP March 11, 2011 14 Comments

If you allow your users can participate your website/service by logging with their current Google Account, that’s really a great idea. The PHP script below will let you know how to do this.

<?php
require 'openid.php';
try {
    $openid = new LightOpenID;
    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());
        }
?>
<a href="<?php echo $_SERVER['PHP_SELF'] . "?login"?>">Login with Google</a>
<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
		if($openid->validate())
		{			
			echo 'User <b>' . $openid->identity . '</b> has logged in.<br>';
 
			echo "<h3>User information</h3>";
 
			$identity = $openid->identity;
			$attributes = $openid->getAttributes();
			$email = $attributes['contact/email'];
			$first_name = $attributes['namePerson/first'];
			$last_name = $attributes['namePerson/last'];
 
			echo "mode: " . $openid->mode . "<br>";
			echo "identity: " . $identity . "<br>";
			echo "email: " . $email . "<br>";
			echo "first_name: " . $first_name . "<br>";
			echo "last_name: " . $last_name . "<br>";
		}
		else
		{
			echo 'User ' . $openid->identity . 'has not logged in.';
		}
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>

Note: I’m using LightOpenID, An PHP 5 library for easy openid authentication. Works only as a consumer.

Instructions:

1. After click on the link “Login with Google”, your browser will take you from the site you’re visiting to the Google Account login page and ask you enter your Google email account and password.

google-openid-login-page

2. After logged successfully, Google will ask to confirm that you’re willing to share your information with your visiting site.

google-openid-confirm

google-openid-confirm

3. If you choose “No thanks”, the browser sends you back to the site you were visiting and says “User has canceled authentication!”

If you choose “Allow”, the browser sends you back to the site and gives it the information you allowed.

+ Download the source code

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.file22.com/news ales

    It works and is realy simple!

  • http://www.file22.com/news ales

    O, btw, what about yahoo :)

    • hoanhuynh

      Hi Ales,

      I did a try but I’m facing with an issue in PHP configuration on my server, will fix it & get back to you soon.

  • http://wws Vishwas Niranjan

    Hello,
    Nice script – how can we get password please help me …….
    Thanks

    • hoanhuynh

      The password should never return when login by openId.

    • Andrei

      password? If only if would be that easy to hack google accounts. Cmon it should be obvious that Google will not give a user pass that easy.

      Anyway, good script and i’m waiting for Yahoo and Facebook login versions

  • Kurshith

    Neat and nice example

  • http://www.devasto.org video youtube

    Thanks a lot!

  • nairobi guy

    thanks alot, really helpful

  • Heta

    can anyone tell me the $openid->identity for twitter,facebook,linkedIn and windows live.??

  • http://www.facebook.com/people/Ngọc-Hưng-Võ/100001804342502 Ngọc Hưng Võ

    ihhiihi

  • machakkalai

    very thanks

  • akhila gopan

    when I copy this code to my server it shows forbidden error after allowing form gmail
    pls help me

  • priti

    can anyone plz tell me for facebook login