Login with Facebook using PHP SDK v5 : Step by Step

Login with Facebook using PHP
You have seen an feature Login with Facebook on many online websites. Users don’t need to create any account on website or remember the credentials. By using this Facebook login feature, they can easily login in any website.

We have seen many tutorial on this topic but found they are not updated as per Facebook API Update Or many are not working properly. In this post we are going to explain step by step how to create login with Facebook using PHP SDK v5.

Steps to Add Facebook Login in PHP

Step 1 : Create an Facebook App:

Go to the https://developers.facebook.com/apps/ link and click on the ‘Add a New App’ button.

create facebook app

A pop up window appear, input the app display name and contact email id. After submit this form your APP is created and an Dashboard will open.

Step 2: Do APP Settings:

Now click on Settings -> Basic option, here you will find APP ID and App Secret. Keep note this info, need to configured later in Facebook PHP SDK.

Enter App Domains value in this basic setting page and choose relevant category as per your app requirement.

facebook app setting

Now click on the ‘+ APP Platform’ option, its showing just below the same Basic setting page. On click, a pop up will appear. You have to choose which platform you are using for this Facebook Login App.

facebook app platform

For making Website login app, Click on Website option, than add login page web url in it. Save the settings.

After doing this, have to add valid OAuth Redirect Url in Facebook Login -> Settings page. Their is section Client OAuth settings. Put valid redirect URL here of your website. This url belong to where user will go after successful login via Facebook. Below we will explain about Redirect Url ‘callback.php’ script file.

facebook app redirect url

Note: Facebook made changes in app authorization process. Now you can access app via HTTPS domain only. Make sure you add secure website in App Settings.

By default your created App is in development mode. To make it live you have to insert Private Policy and Term of service URL in app setting page. These web URL are required to make it LIVE.

Click on App Review option in left menu. Here you find Make WebLogin app public option. Click on button to make it public. Now Facebook developer app setting part is done.

make facebook app live

Step 3: Download the Facebook PHP SDK v5

In next step download the Facebook PHP SDK kit from GitHub. We need only ‘src’ folder to implement login with Facebook feature.

Upload the ‘src’ folder files in your web server.

Step 4: Implement Script Files

Here is the File structure we need to create:

  • index.php
  • fblogin.php
  • callback.php

First create an ‘index.php’ file to create an ‘Login with Facebook’ action button and pointed to ‘fblogin.php’ file. Here is the code below:

Now create ‘fblogin.php’ file to handle Facebook login request. In this we have included uploaded Facebook PHP SDK file.

Configure your App ID and Secret Key in this file, also mention same Redirect URL which you added in Facebook Login valid OAuth Redirect URL setting.

Next we need ‘callback.php’ file to use as a Redirect URL. As above we have explained where you need to put this valid Redirect URL in App Settings.

After successful login with Facebook, user will redirect to this Web URL and show logged in user Facebook account Full Name, Email and ID etc…

In this file we are using getGraphUser() function to get user Facebook account information. You can use any graph api calls in the same way.

Conclusion

So, put this Login with Facebook feature on your website or app and make it more interactive. Hope this tutorial will help you to implement Login with Facebook using PHP SDK v5. If you are facing any issue to add this feature, leave comment below. Thanks

View live demo and Free source code also available to Download.

DEMO

Leave a Comment