Categories: Php

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.

 
<?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-xhr="#">
      <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> </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 :

Related Post

Step 3: To create home page to redirect after successful login home.php.

 
<?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 Successful Login</div>
You are Sign In with : <strong><?=$_SESSION['auth'] ?></strong> Account, User ID:  <strong><?=$_SESSION['email'] ?></strong></td>
  

Result


Step 4: To create sign-out page to redirect after successful logout signout.php.

 
<?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
Tags: php tutorial

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories