Website Login With Google or Yahoo/WordPress/OAL Account

Website login is very important part of web development, now era is change,so no person wants to fill registration form to login in website,You can solve this problem using OpenID. OpenID provides a safe and simple way for people to login into your website/blog without fill a registration form.You can use this code for other account as well like wordpress, yahoo aol ext except Facebook and twitter.You just need an account of OpenID provider to access login without registration form.

I will show you how to connect with Google and yahoo using OpenID provides.

How to Connect Google with OpenId

Step 1:

Download auth file from below url.
Download OpenID

Step 2:

To create login page to get action index.php.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php session_start();
# include google lib file.
require 'openid.php';

try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('localhost');
    
        $openid->required = array(
        'namePerson',
        'namePerson/first',
        'namePerson/last',
        'contact/email',
        );

    if(!$openid->mode) {
        
    if(@$_GET['auth']=="google")
    {
        $_SESSION['auth']="Google";
        $openid->identity = 'https://www.google.com/accounts/o8/id';
        header('Location: ' . $openid->authUrl());
    }elseif(@$_GET['auth']=="yahoo")
    {
        $_SESSION['auth']="Yahoo";
        $openid->identity ='yahoo.com';
        header("Location:".$openid->authUrl());
    }

    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
         $external_login=$openid->getAttributes();
         $_SESSION['name']=$external_login['namePerson/first']." ".$external_login['namePerson/last'];
         $_SESSION['email']=$external_login['contact/email'];
         header("Location:home.php");
         exit();
         
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>

<h1 class="center">Phpflow.com login</h1>
<table width="200" border="0" align="center" cellpadding="5" cellspacing="0" style="border:1px solid #CCC;">
  <tr>
    <td><form id="form1" name="form1" method="post" action="">
      <table width="100%" border="0" cellspacing="0" cellpadding="5">
        <tr>
          <td>E-mail address</td>
          <td><label>
            <input type="text" name="user" id="user" />
          </label></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><label>
            <input type="text" name="pass" id="pass" />
          </label></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><label>
            <input type="submit" name="button" id="button" value="Submit" />
            </label></td>
        </tr>
        </table>
    </form></td>
  </tr>
  <tr>
    <td bgcolor="#F6F6F6">
    <a href="index.php?auth=google"> <img src="Google-login-button.png" alt="google Login"  border="0" /></a>
    <a href="index.php?auth=yahoo">  <img src="http://l.yimg.com/a/i/reg/openid/buttons/17.png" alt="yahoo Login"  border="0" /></a>
    </td>
  </tr>
</table>

Final Result :

Website login with google account/yahoo account

Step3:

To create home page to redirect after successful login home.php.

Select Code
1
2
3
4
5
6
7
8
9
10
<?php session_start();
if(!isset($_SESSION['auth']))
{
    header("Location:index.php");
    exit();
}
?>
<span><a href="sign-out.php">Sign Out</a></span>
<div>Account Home : After Successfull Login</div>
You are Sign In with : <strong><?=$_SESSION['auth'] ?></strong> Account, User ID:  <strong><?=$_SESSION['email'] ?></strong></td>

Result

Website login with google account/yahoo account

Step4: To create signout page to redirect after successful logout (signout.php).
Select Code
1
2
3
4
5
<?php session_start();
session_destroy();
header("Location:index.php");
exit();
?>

Other OpenId Options

There are other account providers also available,You just have to change the url $openid->identity() function by below url:

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