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.
2. After logged successfully, Google will ask to confirm that you’re willing to share your information with your visiting site.
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.