Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Login Or Sign In With Google Account By OpenID

PHP Login Or Sign In With Google Account By OpenID

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();
}
?>

<?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

Mar 11, 2011Hoan Huynh
PHP Auto Post A Story Content To Digg, StumbleUpon, Tumblr and LinkedInLinux Find Out PHP/ MySQL/ MySQLD/ HTTPD ... Command File Location
You Might Also Like:
  • How To Track Website With Multiple Google Analytisc Accounts
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • Get Image Width Height With JQuery And JavaScript
  • Jquery checkbox checked
  • Javascript Problem Set focus textbox on Firefox
  • Display User Current Location On Google Map
  • JavaScript Get Radio Button Value
  • Show Error Message On WordPress Custom Login Template Page
  • Javascript generate a random number using Math.random
  • innerText May Not Work On Mozilla Firefox
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago PHPGoogle, LightOpenID, OpenID320
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,444 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,824 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,638 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,982 views
C# Read Json From URL And Parse/Deserialize Json
11,694 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development