How to implement social media login in web project or application using PHP?

Back to Blog
Ezeelive Technologies - PHP Social Login

How to implement social media login in web project or application using PHP?

Today in this high technology world everyone for their business,  making the websites and increasing their business day by day.

But the main thing is that people don’t want to sign up on every website just to use your service or to buy the product, all wanting the easy and simple login or signup process. Actually, this social login works as a login for you also it works as a signup for the third party.

Like if you want to buy any product from the Amazon or Flipkart but you don’t want to follow the signup instructions then by using your Facebook or Google login credentials you can log in to these websites and can use their services.

So, here I will guide you how you can implement the logic of the third party in your project which also helps to increase the traffic on the website.

Step-by-Step instructions to implement the Google, Facebook login on your web application :

Google Login:

The OpenID library provides a safe, elegant and easy way to login to your website without filling any registration form.

I will show you how to implement it with Google accounts, but the source code is exactly the same for other providers (such as Yahoo!), you just have to change the URLs.

How does it work?

On your website, you have to provide the google link in which user will give the details like:

1. Gives their login credentials ( or if logged then only have to give them access to use the login credentials and some personal details).
Google Sign In Page
2. Give the authorization to google to give you some its personal access to the particular account.
Google Sign in Authentication Page

Once the user clicks on the “Accept” the link redirects to your web-page with login details without any registration form.

Coding Part for Implementation: This code I divided into the 2 part:

Part A: The code to retrieve the user’s personal data from his account and giving the grantor permission to the user to use our website access.

Part B: Code to render the login button. When on our website user clicks the login button it generates one ID which gets added with the respective login access token. Access token changes every time but this grant id remains same for each user.

We will be using a one-file PHP OpenID library that you can download from the google and just put that complete folder in the project file. Here is login.php which where the user will be redirected to the user giving his google or facebook login credentials for the authorization.

require_once 'openid.php';
$openid = new LightOpenID("my-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'];
echo "Identity: $openid->identity <br>";
echo "Email: $email <br>";
echo "First name: $first";
} else {
echo "The user has not logged in";
}
} else {
echo "Go to index page to log in.";
}
?>

Make sure to replace my-domain.com with your domain name and the last step to render the login button lets say you given this file name as index.php.

<?php // index.php
require_once 'openid.php';
$openid = new LightOpenID("my-domain.com");

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

<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>
To test if the user logged in your application or not

session_start();
if (isset($_SESSION['user'])) {
// user logged in
} else {
// user not logged in
}

Facebook Login :

A good and easy way to deal with Facebook authentication is to implement the server side flow with the Facebook PHP SDK. Here is how you do that.

require "facebook.php";
$facebook = new Facebook(array(
'appId'  => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
// The user is logged in
try {
$user_profile = $facebook->api('/me');
// Here : API call succeeded,
// you have a valid access token
} catch (FacebookApiException $e) {
// Here : API call failed,
// you don't have a valid access token
// you have to send him to $facebook->getLoginUrl()
$user = null;
}

After that, if you get the result set in the $user, it means that the user is authenticated. So here is what you can display on your page :

<?php if ($user): ?>
<a href="<?php echo $facebook->getLogoutUrl() ?>">
Logout of Facebook
</a>
<?php else: ?>
<a href="<?php echo $facebook->getLoginUrl() ?>">
Login with Facebook
</a>
<?php endif ?>

When the user is authenticated, you can make API calls with his access token

$user_profile = $facebook->api('/me');

Conclusion :

So, in this way you can allow the user to login with their social media credentials which getting popular day by day. I think it is the simplest way to implement the third party login in your website application by creating the API’s.

Summary
How to implement social media login in web project or application using PHP?
Article Name
How to implement social media login in web project or application using PHP?
Description
Implement social login help to business grow, customer can login without register on system, social media login is used more than 300000 websites worldwide.
Author
Publisher Name
Ezeelive Technologies
Publisher Logo

Share this post

Comments (3)

  • Avatar
    Amruta Patil Reply

    Hi, hello
    I am fresher working in a small startup company and developing the app to share post on the third party i.e i want the user to share their post on our company’s facebook account and not to his personal account and the post should be shared without logging in. So is that possible if so please guide me do so.

    Thank you.

    September 11, 2018 at 5:07 pm
  • Avatar
    sushma Reply

    Hi

    how to show button (continue as username ) if i m logged in google account.

    I have used PHP sdk.

    September 18, 2019 at 1:12 pm
    • Rajeev Sharma
      Rajeev Sharma Reply

      Hi Sushma, You can reach us on info[at]ezeelive[dom]com for any assistance in google login integration.

      September 24, 2019 at 6:22 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Blog