Login Using Google, Yahoo, WordPress and AOL Account into Your Website

Advertisement

As of these days social networking sites are opened use in public users for SEO, Boast organic traffic or even connect in other various site to avoid hassle inputting list of your information. In this tutorial I’ll explain on how to add login using google, yahoo, wordpress and aol account in one same codes, that’s sound simple and easy right?

Okay let’s start.

Download Library

The first thing you need to do is download OpenID library at LightOpenID, just put this file in your project directory.

Login.php

This contains the login button in this example I’ve use Google, this will call the Google window and the site to redirect after login.


<?php
require_once 'openid.php';
$openid = new LightOpenID("http://www.domain.com");

$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array(
  'namePerson/first',
  'namePerson/last',
  'contact/email',
);
$openid->returnUrl = 'http://localhost/index.php'
?>

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>

You need to change domain.com to your domain name and returnUrl to where you want to redirect your site after login.

Index.php

This contains the data after login, email and name, also you need to add identity field in your login table to store the $openid->identity. It is the unique ID that Google provides you to identify the user. The other fields automatically change when the user change his Google profile info or any other OpenID provider.


<?php
require_once 'openid.php';
$openid = new LightOpenID("http://www.domain.com");

if ($openid->mode) {
    if ($openid->mode == 'cancel') {
        echo "User has canceled authentication !";
    } elseif($openid->validate()) {
        $data = $openid->getAttributes();
        $email = $data['contact/email'];
        $first = $data['namePerson/first'];
        $last = $data['namePerson/last'];
        echo "Identity : $openid->identity";
        echo "Email : $email";
        echo "First name : $first";
        echo "Last name : $last";
    } else {
        echo "You're not login";
    }
} else {
    echo "Go to index page to log in.";
}
?>

Logout.php

To logout the user , we only need to destroy all session.


<?php
   session_start();
   session_destroy();
?>

Other OpenID Provider

To login using other OpenID provider you only change the $openid->identity to:

  • Google : https://www.google.com/accounts/o8/id
  • Google profile : http://www.google.com/profiles/~YOURUSERNAME
  • Yahoo : https://me.yahoo.com
  • AOL : https://www.aol.com
  • WordPress : http://YOURBLOG.wordpress.com
  • LiveJournal : http://www.livejournal.com/openid/server.bml

That’s it happy coding ^_^

Advertisement