Login With Facebook in PHP

| By Webner

How to implement Login With Facebook in PHP

In today’s environment it is very common to use facebook and gmail login features to make login process more authentic and fast. Here I am going to describe how to implement login via facebook in your web application.

There are two main steps:
1. Create facebook App Id and App secret on facebook developer console.
2. Add implementation code logic to your code.

Step1 : Create an app using facebook developer console and generate your credentials. With the help of these credentials you are authorized to access facebook api calls from your application.

Steps to create the App ID and App Secret-
1. Login to the URL – https://developers.facebook.com/
2. After Logging in click on the Add New App link.
3. Clicking on that link will open a Pop Up as described below.
Login With Facebook in PHP
Here we need to write the display name of our application.

4. Now after clicking on the Create App ID button it will create App ID and App Secret for our application.
5. Add your CLIENT_REDIRECT_URL in the facebook app dashboard
Login With Facebook in PHP

6. Define all the domains that your web application has
Login With Facebook in PHP

For example if you have a test site – https://www.example.com
Then our domain will be www.example.com or example.com without any https etc.

Step2: Add implementation code logic to your code
We have implemented the code logic by using rest api calls provided by facebook directly. Facebook also provides an sdk for this. You can use any method which is suitable to your needs.

1. Get Authentication Code: Add below link to your code where you want to use facebook login.

<a href='https://www.facebook.com/v3.0/dialog/oauth?client_id=APP_ID?redirect_uri=CLIENT_REDIRECT_URL&auth_type=rerequest&scope=public_profile,email' >Login with Facebook</a>

client_id is the id of our app on facebook.
redirect_uri is the redirect url at which user will be redirect after authenticating with facebook.
scope is used to tell the facebook what we want from the user for him to use our application. Public_profile is default scope and we can give as many scopes as we want separated by a comma.
auth_type It defines the authentication type. (Means when we ask a user for infomations then the user might deny for one or more permissions, so if the auth_type is rerequest then it will ask for those denied permission when user logins to our website again using the facebook Login).

This api request will return the authentication token which authorizes us to access facebook. Authentication code confirms you as a valid FB user. We can get that code like this $authenticationCode=$_GET[‘code’];

2) Generate Access Token : After getting the authenticationCode from the facebook we will need to make a HTTP GET request to get user access token.

$httpRequestURL= 'https://graph.facebook.com/v3.0/oauth/access_token?client_id=APP_ID&redirect_uri=CLIENT_REDIRECT_URL&client_secret=APP_SECRET&code=$authenticationCode;

client_id It is the app ID.
redirect_uri is the redirect url at which user will be redirect after authenticating with facebook.
client_secret It is the app secret ID.
code It is the code that we received in response from the facebook.

$accessToken= $getAccessToken>access_token;

Now this access token can be used to make a request to the facebook to get the information about the user. Access token means you as a FB user are giving access to the web application for certain resources.

3) Get User Details :-

$get_user_details = "https://graph.facebook.com/me?fields=email,first_name,last_name,gender&access_token=" .$accessToken;

fields It is used to manually define that what information we need like name, gender and email etc.
access_token It indicates a secure connection between our app and the facebook.

$response = file_get_contents($get_user_details);
            $response = json_decode($response);

This was all about how we can make a facebook login flow manually without using any facebook library in our code.

Leave a Reply

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