Facebook Login using PHP – Web SDK

Abhigyan Singh 29th Sep 2020

This tutorial will explain about login on Facebook using PHP. Here we use Facebook web SDK for Facebook login.

Facebook login on any website is a convenient and secure way to register users instead of registering with the long registration forms. Most of the websites are using Facebook login facilities to register users and can track easily user basic details. A user who comes on the website prefers to login with Facebook to get a new update of the website. It has been necessary to integrate Facebook login into the website to increase website traffic. To engage more users on the website you can need to add login with Facebook to register users.

Here, we will tell you how you can add a Facebook login button to your website with PHP SDK.facebook web SDK is a set of tools that can be used to develop software applications targeting a specific platform. For adding the Facebook login button, the process will be as follows:

  • Create New App on Facebook developer dashboard to get the app id, secret key, and redirect URL and put these all in the fb_config file.
  • Download SDK files for PHP and Create View file for showing the login button and for retrieving app requests or responses.

Create Facebook App

Below, we are going to describe the whole process in details or step by step:

1. Go to https://developers.facebook.com/apps/  then you will see Add New App.

2. By Clicking Add new App Button, Pop up window will appear to choose your platform. Choose your platform to Websites as shown below.

3. After choosing the platform it will redirect to the quick start page as shown below. Enter your app name what you want. In my case, the app name is test3. The name will show when a user will log in through your app.

4. Click on create new Facebook app id, you will redirect to a quick overview page as shown below. Enter your redirect URL where you want to redirect to the user after submitting details.
After clicking the next button your app will be created. To see your app details click on the Skip Quick Start button will have a dashboard page.

5. App Dashboard page will look as shown below. Here you can change your redirect URL and app domain by going in setting and can also apply for other user permission like a public post, the page which Facebook gives after review your app. You can also share this app with others or define app roles according to their profile.

Download PHP SDK and merge with your Project

You can get PHP SDK Files from inc folder when you download our demo and can modify according to your requirement as shown below:

After downloading, you have to create three files:

  • index.php
  • fb_config.php
  • fb_logout.php

In index.php file will contain your view part i.e when the user will log in through the button or some hyperlink and retrieving the app request or response .put following code in the index file and save it.

<?php
error_reporting(0);
include_once("fb_config.php");
//destroy facebook session if user clicks reset
if(!$fbuser){
$fbuser = null;
$fbloginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
$result = '<a href="'.$fbloginUrl.'"><img src="image/facebook.jpg"></a>';
}else{
$user_data = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');

if(!empty($user_data)){
$result = '<h1>Facebook Profile Details </h1>';
$result .= '<img src="'.$user_data['picture']['data']['url'].'">';
$result .= '<br/>Facebook ID : ' . $user_data['id'];
$result .= '<br/>Name : ' . $user_data['first_name'].' '.$user_data['last_name'];
$result .= '<br/>Email : ' . $user_data['email'];
$result .= '<br/>Gender : ' . $user_data['gender'];
$result .= '<br/>Locale : ' . $user_data['locale'];
$result .= '<br/>Logout from <a href="fb_logout.php?logout">Facebook</a>';
}else{
$result = '<h3 style="color:green">Please try again.</h3>';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook using PHP</title>
</head>
<body>
<div>
<?php echo $result; ?>
</div>

</body>
</html>

In fb_config.php, you will put your app id, secret id and redirect URL and save with the following code:

<?php
include_once("sdk/facebook.php"); //include facebook SDK
######### Facebook API Configuration ##########
$appId = App ID' '; //Facebook App ID
$appSecret = 'App Secret'; // Facebook App Secret
$homeurl = 'Redirect URL '; //Redirect URL
$fbPermissions = 'email'; //Required facebook permissions

//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret

));
$fbuser = $facebook->getUser();
?>
By using logout.php file a user can logout from fb account and will be redirect to index page as shown code below :
<?php
include_once("fb_config.php");
if(array_key_exists('logout',$_GET))
{
$facebook->destroySession();
session_start();
unset($_SESSION['userdata']);
session_destroy();
header("Location:index.php");
}
?>

If you have any queries regarding my script, please feel free to ask me.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk