Get latest Update ! Subscribe Now !

How to login with Facebook SDK using PHP or Javascript ?

Today, web owners want to create their website as robust, how quickly users subscribe to websites. Nowadays users want an easy way to login or subscribe to the website. If the website has asked the user to input their details to subscribe which is a very cumbersome job to the user, because of this issue user are not going to log in to your website, to overcome these issues we can use the login with Facebook using PHP or Javascript which helps in to reduce the extra activity to subscribe the websites. With just a single click user can subscribe to your website using login with Facebook.

There are Two way to achieve in this tutorial :

  1. Login with Facebook using PHP (Server-Side) ?
  2. Login with Facebook using JavaScript (Client-Side) ?

How to login with Facebook using PHP ?

In this tutorial, Uses the PHP with Facebook graph API v9.0 , PHP SDK of Facebook Graph API allows to perform the all the required task needed. Use latest Graph API version of Facebook API, you can get the from click me This tutorial using the Facebook Graph API V9.0 with PHP SDK, To implement the Registration Process or Login with Facebook.

Required Facebook graph API v9.0 :

  • PHP version should be 5.6 or greater.
  • Facebook PHP SDK ( Get it from php-graph-sdk )

Structure of PHP Pages, that we use to login with Facebook using PHP:

directory php

Create Facebook Application:

STEP 1: First, need to create a Facebook Developer Account Click me .
STEP 2: Click on My Apps and login with your Facebook Account on the Top-Left Navigation Bar of Facebook For Developers website.

facebook myapp

STEP 3: You will get as shown below, If you are not getting then you need to create Facebook Developer Accounts. Then you will get as shown below, Now click on Create App :

facebook developer

STEP 4: Select the Category from given options shown below, In this Tutorial using Manage Business Integration and click continue button :

facebook Manage Business

STEP 5: Now, Fill in the Required Information field for your Application, You need to enter the Following Data:
  1. App Display Name
  2. App Contact Email
  3. App Purpose
  4. Business Manager Account (Optional)

facebook Application
  After filling required information click on Create App button.

STEP 6: You can see the App Dashboard something like this :

facebook App Dashboard
  Note - Add Additional Products to Your App as shown in Pictures.

STEP 7: Now, click on Facebook Login from Sidebar menu then navigate to Settings, you need to enter your Redirect_URI_FACEBOOK inside the Valid OAuth Redirect URIs Field as you can see below:

facebook Redirect_URI_FACEBOOK

STEP 8: Click on Settings from Side-Menu and go to the Basic, To use this app in your website you need to add the add your domain (e.g to the domains field as well as the privacy page URL of your website ( e.g: ) inside the field of Privacy Policy URL then click on Save Changes as shown below :

facebook domain

STEP 9: Remember the APP ID and Secret Key that is used to authenticate your website to login the User :

facebook APP ID and Secret Key

STEP 10: Now, We are ready but before start we need to set the Facebook Permission , Click on App review on Side Menu Bar then navigate to the Permission and Features . You can see the list of Access Level of Facebook API, there are some basic things that you can access without any review for example public_profile and email , If you need to manage the Posts of user with your website then you need to send the Request of Advanced Access for your website then Facebook developer will review your requests application, For a review you need to submit the additional information of your website you can get from here click me, In this tutorial, we are working on Basic Access.

Facebook Permission

Setting website and Config.php files :

Now, You have your own App ID as well as Secret Key, fill the data with your App Id and Secret Key.

Login and Handle Facebook user data in index.php file :

Index.php file handle the login process whether the user authenticated or not ? If the user authorized then Login to this PHP page and then handle the User Data. In this file First it check whether the access token of Facebook exist in $accessTokenFB Variable or not, if it exist in $accessTokenFB variable then simply Login otherwise get the Login URL of Facebook using getLoginUrl() function of Facebook Graph API handler. The getLoginUrl() helps in to get the login URL using the App credentials by which user can able to login into your website. Once user logged in with Facebook Account they can able to logout with URL generated by function of Facebook Graph API using getLogoutUrl() Function . In case there are some issues then you will get the error message something like “Unable to login due to technical error….”.

Use the $_SESSIONS variable to store user’s data ( Facebook using PHP ) :

Uses the Following $_SESSIONS to store the Tokens as well as user data:
  1. $_SESSION['facebook_access_token']
  2. $_SESSION['imgURL']
  3. $_SESSION['id']
  4. $_SESSION['email']
  5. $_SESSION['first_name']
  6. $_SESSION['last_name']

Create Logout.php File :

Assume, user logged in using Facebook Account to your website, now we need to add the Logout PHP file as shown below that helps in to unset or clear all the SESSION variables by which user can logout from your website when needed.

Output :

Output 1:

facebook login output php

Output 2:

facebook login output  php

Conclusion :

In this tutorial, we learned about how the Facebook Graph API works and how we can use it with PHP, by which users can subscribe to the website as well as login website with a Facebook account. We also cover how to use this technique to improve the subscription of the website.