Aftab Muni

PHP, API Creation, Wordpress Theme Development, Wordpress Custom Plugin Development, Wordpress Theme Customization, Magento, Joomla, HTML5/CSS3, JQuery, Responsive UI Desginer…

  • Facebook
  • LinkedIn
  • Skype
  • Twitter
  • Google Plus

Widgets

Search

Skip to content

Best Multi-purpose WordPress Theme

NewsPaper 9

Visitors

Total Visitors

Categories

  • Amazon Web Services
  • CodeIgniter
  • Extras
  • HTML/CSS
  • JQuery
  • Magento
  • MYSQL
  • PHP
  • Web Services
  • Woocommerce
  • Wordpress

Recent Posts

  • Disable Right Click, View Source, Inspect Element For WordPress
  • WordPress add active class to Menu item – Plugin
  • WP Admin Logo Changer – Plugin
  • Indian Rupee Symbol For Woocommerce – Plugin
  • How to convert number to words in PHP

Archives

  • November 2018
  • February 2017
  • January 2017
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • September 2014
  • August 2014

google login

Login With Facebook Twitter and Google using PHP

February 24, 2016aftabmuni 3 Comments

social

In this tutorial, I have explained how to add Login with Facebook, Twitter and Google functionality to your website using PHP. I have used HybridAuth PHP library to achieve this.

List of identity providers supported by HybridAuth Library.

  • Facebook
  • Twitter
  • Google
  • Yahoo
  • LinkedIn
  • Live
  • AOL
  • MySpace
  • Github

Follow the below steps to create Login with Facebook,Twitter and Google widget. Below image explains how hybridAuth library works.

flowchart

Step 1) Download HybridAuth PHP library from Github .

Step 2). You need to get Developer API(OAuth) Key and Secret from Facebook, Twitter and Google.

Step 3). Create config.php and add below code to the file.

$config = array("base_url" => "YOUR_WEBSITE_OAUTH_URL",
        "providers" => array (
            "Google" => array (
                "enabled" => true,
                "keys"    => array ( "id" => "YOUR_GOOGLE_API_KEY", "secret" => "YOUR_GOOGLE_API_SECRET" ),
            ),
            "Facebook" => array (
                "enabled" => true,
                "keys"    => array ( "id" => "FACEBOOK_DEVELOER_KEY", "secret" => "FACEBOOK_SECRET" ),
                "scope" => "email, user_about_me, user_birthday, user_hometown"  //optional.             
            ),
            "Twitter" => array (
                "enabled" => true,
                "keys"    => array ( "key" => "TWITTER_DEVELOPER_KEY", "secret" => "TWITTER_SECRET" )
            ),
        ),
        // if you want to enable logging, set 'debug_mode' to true  then provide a writable file by the web server on "debug_file"
        "debug_mode" => false,
        "debug_file" => "debug.log",
    );

Note: Set API key and secrets for Google, Facebook and Twitter. base_url is the OAuth Callback page. In demo page, base_url=`http://dev.social-login.com/examples/oauth/hybridauth/hybridauth/index.php` Content of the base_url is.

<?php
require_once( "Hybrid/Auth.php");
require_once( "Hybrid/Endpoint.php");
Hybrid_Endpoint::process();
?>
Note: Set your Google API OAuth Settings redirect URL like this http://dev.social-login.com/examples/oauth/hybridauth/hybridauth/index.php?hauth.done=Google

Step 4). Create login.html and add below code

<html>
<body>
    <a href="login-with.php?provider=Facebook">Login With Facebook</a>
    <a href="login-with.php?provider=Twitter">Login With Twitter</a>
    <a href="login-with.php?provider=Google">Login With Google</a>
 </body>
</html>

Step 5) Create login-with.php and add below code

<?php
session_start();
include('config.php');
include('hybridauth/Hybrid/Auth.php');
if(isset($_GET['provider']))
{
$provider = $_GET['provider'];
try{
    $hybridauth = new Hybrid_Auth( $config );
    $authProvider = $hybridauth->authenticate($provider);
    $user_profile = $authProvider->getUserProfile();
    if($user_profile && isset($user_profile->identifier))
    {
        echo "<b>Name</b> :".$user_profile->displayName."<br>";
        echo "<b>Profile URL</b> :".$user_profile->profileURL."<br>";
        echo "<b>Image</b> :".$user_profile->photoURL."<br> ";
        echo "<img src='".$user_profile->photoURL."'/><br>";
        echo "<b>Email</b> :".$user_profile->email."<br>";
        echo "<br> <a href='logout.php'>Logout</a>";
    }          
    }
    catch( Exception $e )
    {
         switch( $e->getCode() )
         {
                case 0 : echo "Unspecified error."; break;
                case 1 : echo "Hybridauth configuration error."; break;
                case 2 : echo "Provider not properly configured."; break;
                case 3 : echo "Unknown or disabled provider."; break;
                case 4 : echo "Missing provider application credentials."; break;
                case 5 : echo "Authentication failed The user has canceled the authentication or the provider refused the connection.";
                         break;
                case 6 : echo "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.";
                         $authProvider->logout();
                         break;
                case 7 : echo "User not connected to the provider.";
                         $authProvider->logout();
                         break;
                case 8 : echo "Provider does not support this feature."; break;
        }
        echo "<br /><br /><b>Original error message:</b> " . $e->getMessage();
        echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>";
    }
}
?>

Step 6) Create logout.php with the below code.

<?php
session_start();
session_destroy();
header("Location: login.html");
?>

From: http://hayageek.com/

facebook logingoogle loginlinkedin loginsocial logintwitter login
Create a free website or blog at WordPress.com.
  • Subscribe Subscribed
    • Aftab Muni
    • Already have a WordPress.com account? Log in now.
  • Privacy
    • Aftab Muni
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar

Loading Comments...

    Design a site like this with WordPress.com
    Get started