Facebook Login using PHP - Web SDK

This tutorial will explain about login in facebook using php. Here we use facebook web SDK for facebook login.

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

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

  • Create New App on facebook developer dashboard to get app id, secret key and redirect url and put these all in fb_config file.
  • Download SDK files for php and Create View file for showing login button and for retrieving app request or response.

Create Facebook App

Below, we are going to describe 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 platform it will redirect to quick start page as shown below. Enter your app name what you want. In my case, app name is test3.Name will show when a user will login through your app.


4. Click on create new facebook app id, you will redirect to quick overview page as shown below. Enter your redirect url where you want to redirect to user after submitting details.
After clicking next button your app will be created. To see your app details click on Skip Quick Start button will have 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 others user  permission like public post, page  which facebook gives after review your app. You can also share this app to other 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 user will login through button or some hyper link and retrieving app request or response .put following code in index file and save it.

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

		$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>'; 
		$result = '<h3 style="color:green">Please try again.</h3>';
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook using PHP</title>
<?php echo $result; ?>



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

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 :


If you have any query regarding my script, Please feel free to ask me.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.