This is one more post on How to make Social Login in PHP. In this post, we will use Facebook account login allow to user for get access into our website. Here you can find process for login with Facebook PHP SDK library. In this tutorial, we have use latest Facebook API SDK library for login with PHP script.
In most of website, you can generally find registration form for sign up for get access into website, but most of the user is not interested in filling large website registration form. At this time if you have allow user to login or register with Facebook login, then user has not required to filling large registration form and they can easily get access into website by login using Facebook account. In this blog, we will learn you how to implement login with Facebook account in your PHP website.
Here we have use Facebook PHP SDK library that allows to access Facebook API from your PHP web application. By using this library you can simply integrate the PHP Login with Facebook Account using Facebook SDK library. Here you can find process for how to make User login and registration system with Facebook using PHP and display user profile data on web page.
Now we have get started with the latest version of Facebook SDK v5.x and your system must required following configuration.
- PHP version must be greater than 5.4.
- mbstring extension must be enable in your server.
Create Facebook APP
For get access Facebook API, first you need to create Facebook app and then get App ID and App Secret which you can use at the time of calling Facebook API. Below you can find step by step process for create Facebook app in Facebook developers dashboard.
1. First go to https://developers.facebook.com/ facebook developers page and login with your Facebook Account.
2. After login into Facebook account, now click on My apps menu and in this click on Create App link.
3. After click on Create App link, then modal has pop up on web page, here you have to define App Display Name details and click on Create App ID button.
4. Now navigate to Setting » Basic link.
5. Here you have to specify App Domains, Privacy Policy URL, Terms of Service URL and select Category. Lastly, you have to click on Save Changes button.
6. Now you have to navigate to Add a Product page by click on plus button. So, modal will pop up and here you have to select Website.
7. After select Website option, then Website field has been appear on web page, and here you have to define your app Site URL.
8. Now you have to add product, so go to Dashboard link and on you have to Set up Facebook Login product.
9. Once you have Set up Facebook Login product, then new web page has been load, here you have to define Valid OAuth Redirect URIs and click on Save Changes button.
10. You have change your status from In Development mode to Live mode, for this you have to click on Off button, after click on Off button, then modal will pop up and here you have to select Category and click on Switch Mode button. After click on Switch Mode button, your App will be Live this is required for get profile data from Facebook API.
Download Facebook SDK for PHP
After this we want to download Facebook SDK for PHP library. For this we have to open Command Prompt editor. In this first you have to go to directory in which you want to download Facebook SDK for PHP. After this you have to run Composer command, this is because we have use Composer dependecy manager for download Facebook SDK for PHP. After this you have to run following command.
composer require facebook/graph-sdk
This command will download Facebook SDK library for PHP in define directory.
config.php
In this file first we need to add autoload.php file of Facebook SDK library for PHP using require_once statement.
After this, we have to check, session variable started or not by using PHP session_id() function.
Now we have to called Facebook API, here we have to define app_id and app_secret key which we have to get at the time of create Facebook App. Here we have to define default_graph_version also.
<?php
require_once 'vendor/autoload.php';
if (!session_id())
{
session_start();
}
// Call Facebook API
$facebook = new \Facebook\Facebook([
'app_id' => '921130358246916',
'app_secret' => '8d382dc63a190925664594ef090b8a78',
'default_graph_version' => 'v2.10'
]);
?>
index.php
In this file first we need to add config.php file by using include statement.
After this we want to get redirect login helper, for this here we have use getRedirectLoginHelper() method.
Now we want to check any $_GET['code'] variable value has been received or not, if received that means user has login using Facebook account and display profile data on web page, otherwise display link for Login using Facebook account.
Suppose it has received $_GET['code'] variable value, then we want to check access token has been store in $_SESSION variable or not if it is already store in $_SESSION variable, then that value has been store in local variable.
But suppose access token is not get from $_SESSION variable, then we need to get by using getAccessToken() method and store in local variable and then after store in $_SESSION variable.
After getting access token now we want to set the default access token to use with request by using setDefaultAccessToken() method.
Now we have proceed for get profile data, for this here we have use get() method and under this method we have to define profile fields like name, email etc and in second argument we have to define access token value.
Lastly, for get user profile data, here we have use getGraphUser(), this method will creating a graph user collection.
This way we can get data from Facebook API and store in $_SESSION variable.
For create link for login using Facebook account, for this here we have use getLoginUrl() method, this method will send the user in order to login to Facebook. In this method you have to define redirect url and permission array.
So, below you can source code for get login with Facebook account using PHP and display profile data on web page.
<?php
//index.php
include('config.php');
$facebook_output = '';
$facebook_helper = $facebook->getRedirectLoginHelper();
if(isset($_GET['code']))
{
if(isset($_SESSION['access_token']))
{
$access_token = $_SESSION['access_token'];
}
else
{
$access_token = $facebook_helper->getAccessToken();
$_SESSION['access_token'] = $access_token;
$facebook->setDefaultAccessToken($_SESSION['access_token']);
}
$_SESSION['user_id'] = '';
$_SESSION['user_name'] = '';
$_SESSION['user_email_address'] = '';
$_SESSION['user_image'] = '';
$graph_response = $facebook->get("/me?fields=name,email", $access_token);
$facebook_user_info = $graph_response->getGraphUser();
if(!empty($facebook_user_info['id']))
{
$_SESSION['user_image'] = 'http://graph.facebook.com/'.$facebook_user_info['id'].'/picture';
}
if(!empty($facebook_user_info['name']))
{
$_SESSION['user_name'] = $facebook_user_info['name'];
}
if(!empty($facebook_user_info['email']))
{
$_SESSION['user_email_address'] = $facebook_user_info['email'];
}
}
else
{
// Get login url
$facebook_permissions = ['email']; // Optional permissions
$facebook_login_url = $facebook_helper->getLoginUrl('https://greenhouses-pro.co.uk/demo/', $facebook_permissions);
// Render Facebook login button
$facebook_login_url = '<div align="center"><a href="'.$facebook_login_url.'"><img src="php-login-with-facebook.gif" /></a></div>';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Login using Google Account</title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">PHP Login using Google Account</h2>
<br />
<div class="panel panel-default">
<?php
if(isset($facebook_login_url))
{
echo $facebook_login_url;
}
else
{
echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
echo '<h3><b>Name :</b> '.$_SESSION['user_name'].'</h3>';
echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
echo '<h3><a href="logout.php">Logout</h3></div>';
}
?>
</div>
</div>
</body>
</html>
logout.php
<?php
//logout.php
session_destroy();
header('location:index.php');
?>
So, from this post you can learn how to allow user to login using Facebook account in PHP website, We hope you have learn login with Facebook in PHP web application from this post.
Thank you!
ReplyDeleteThanks
ReplyDeletegood
ReplyDeletehello, Can you email this lession files please, Thank you
ReplyDeleteKasunmadawa.lk@gmail.com
i have an existing website how to impliment this script near to existing login page
ReplyDeleteBro I didn't tried this one but as far I tried your codes they are really helpful.
ReplyDeleteThank You for making such website
fine...
ReplyDeleteWell guided content to help new users!
ReplyDeletethanks!
please change text color in the post to black & increase some font size. Its difficult to read.
ReplyDeleteNice work how can i implement a match fixture and standings team profile in php and mysql
ReplyDeleteThank you
ReplyDeleteIt works but then when I get logged in and refresh page I get this "hash_hmac() expects parameter 2 to be string, object given in /var/www/html/mysite/public_html/vendor/facebook/graph-sdk/src/Facebook/Authentication/AccessToken.php on line 70"
ReplyDeleteSir mera page nhen ban rha
ReplyDeletewhy $facebook-output = ''; variable?
ReplyDeleteThanks bro
ReplyDeletethanks for tutorial i used this in my websit https://www.indiahouselifting.com
ReplyDeletehello
ReplyDeleteHi
ReplyDeleteThx
ReplyDelete