Get latest Update ! Subscribe Now !
There are Two way to achieve in this tutorial :
- Login with Facebook using PHP (Server-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:
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.
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 :
STEP 4: Select the Category from given options shown below, In this Tutorial using Manage Business Integration and click continue button :
STEP 5: Now, Fill in the Required Information field for your Application, You need to enter the Following Data:
- App Display Name
- App Contact Email
- App Purpose
- Business Manager Account (Optional)
After filling required information click on Create App button.
STEP 6: You can see the App Dashboard something like this :
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:
STEP 9: Remember the APP ID and Secret Key that is used to authenticate your website to login the User :
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.
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:
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.
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.