Login With Facebook Using PHP SDK

We all tolerate Registering on a website because of the registration form,and when we see Facebook Button,Google Button,Twitter Button or any another Social network button,we quickly signup.So in this tutorial i will tell you how you can integrate login with facebook button in your web project.
Login With Facebook using PHP SDK

View Demo Download

Login with Facebook

Before we start to integrate facebook login, we have to create an Facebook App for our website.

Create an Facebook App

Here are few steps for creating Facebook App.
Step 1: Go to https://developers.facebook.com/apps/ and Click on the Create a New App button.
Create a New App Button
Step 2:Fill you App Details and Click on the Create App Id button.
Create a New App ID
Step 3: Now, Copy your App Id and App Secret(Click on the Show button to show the app secret).
App Details

Integrate the Facebook SDK

Step 1: Download Our File and extract it.
Step 2: Go to fbconfig.php file and edit it.

require 'src/facebook.php';  // Include facebook SDK file

$facebook = new Facebook(array(
  'appId'  => 'YOUR APP ID',   // Facebook App ID
  'secret' => 'YOUR APP SECRET',  // Facebook App Secret
  'cookie' => true,

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  	    $fbid = $user_profile['id'];            // To Get Facebook ID
 	    $fbuname = $user_profile['username'];  // To Get Facebook Username
 	    $fbfullname = $user_profile['name'];   // To Get Facebook full name
	    $femail = $user_profile['email'];    // To Get Facebook email ID
/* ---- Session Variables -----*/
	    $_SESSION['FBID'] = $fbid;
            $_SESSION['USERNAME'] = $fbuname;
            $_SESSION['FULLNAME'] = $fbfullname;
	    $_SESSION['EMAIL'] =  $femail;
  } catch (FacebookApiException $e) {
   $user = null;

if ($user) {
	header("Location: index.php");
} else {
 $loginUrl = $facebook->getLoginUrl(array(
		'scope' => 'email,public_profile',  // Permissions to request from the user
 header("Location: ".$loginUrl);

Replace YOUR APP ID with your App Id you copied from Your Facebook App.
Replace YOUR APP SECRET with your App Secret you copied from Your Facebook App.
If you want to add some more scope then you can get it from https://developers.facebook.com/docs/facebook-login/permissions/ and edit the 33rd Line
Step 3: If you want to change your home page url from logout.php, you can.

unset($_SESSION); //Unset the SESSION
header("Location: index.php");        // you can enter home page here ( Eg : header("Location: http://mycodingtricks.com"); 

Step 4: You can change your index.php file
You HTML should look like…

<!doctype html>
	<title>Your Page Title Here</title>
	<!-- Your Head Code goes Here(e.g: stylesheet,javascript) -->

		<!-- Your HTML Code Goes Here -->
		<!-- When User Logged In-->


		<!-- Your HTML Code Goes Here -->
		<!-- When user Logged out/ Not Logged in -->

View Demo Download

You can learn more about HTML5.

Please don’t forget to share this and subsribe us to get latest updates via email.
If you have any question then ask us through comment.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

10 Responses

  1. Arista says:

    Hi… great article, but I seem to be having trouble downloading the files. I signed up for the newsletter, but it still doesn’t let me download the file. Thanks!

  2. aniket says:

    thank really needed it bro

  3. Virendra says:

    This is a great article and explain everything that I am looking for thanks Shubham Keep it UP.
    I just need one help could you please tell me how this extending token works in java code on server side.

  4. Jason says:


    Great article. Do you know exactly how long this access token will last. I have read that Facebook issues short term and long term ( 2 month ) access tokens.

    • Hey Jason,

      Glad to know it helped you.

      Now coming to your question,
      Both the token may eventually expire so it can’t be predicted that for how much long the access token will be valid.

      Here is the code you can use to extend the expiry of the token or you can say that the token will never expire.

      // Set Extended Access Token
      // Get access to short live access token
      $accessToken = $facebook->getAccessToken();
      // Exchange token
      $facebook->api('/oauth/access_token', 'POST',
              'grant_type' => 'fb_exchange_token',           
              'client_id' => 'FB APP ID',
              'client_secret' => 'FB APP Secret',
              'fb_exchange_token' => $accessToken

      The above code will echo the access token and then debug it using the access token debugger. The result should be: Expires: Never.

      Best Regards,

  5. LO says:

    I do like the way you have framed this specific matter and it does give me personally a lot of fodder for consideration. Nevertheless, coming from what I have personally seen, I only trust when the feed-back stack on that individuals keep on issue and not get started upon a soap box regarding the news du jour. Yet, thank you for this exceptional point and while I do not necessarily agree with this in totality, I respect your perspective.

  6. Sumit says:

    You have explained the new version of Facebook App development.
    Nice article.
    Keep it up.

Leave a Reply

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