PHP Login System Using Facebook SDK in 4 Steps

This is simple Login System using Facebook API. You can use this to enable facebook login’s on your website. Before continuing with our Facebook login PHP, you should have facebook developer account. If you don’t have one create it using above link.

Your server should support these following system requirements

1. Setup Facebook PHP SDK Files

After server requirements next step is setting up Facebook SDK for PHP. There are two methods to set up Facebook SDK for PHP. One method is using composer other method is downloading & installing Facebook SDK for PHP manually from GitHub.

Using the above link, you can download facebook PHP SDK files.

If you want to download with composer. You can follow this step.
Create a file in your project working directory with the name of composer.json.

Then run this code in command line terminal.

If you need any information about getting started with Facebook PHP SDK, then go through this facebook developers documentation.

Then create two files with the names of login.php & fb-callback.php

Add session_start() PHP functions is both the files after opening PHP tag.

Then include PHP Facebook SDK files using this below line of code.
If you have manually downloaded use this code, change the path with correct directory path on your server.

If you have downloaded PHP Facebook SDK using composer, use this below line of code.

2. Create Facebook App from FB Developers

Login to Facebook Developers, if you don’t have an account create one.

From My Apps -> Click on Add New App Link
facebook login php

Enter Display Name, Contact Email, Category. Then click on Create App ID.
facebook login php

After that it will asks for captcha code, enter it and click on submit.
facebook login php

Then you will be redirected to facebook app dashboard. From the dashboard, you can access the Facebook APP ID & Facebook APP Secret. You can use these in application.

Before that you need to configure few things in facebook app.
Click on settings from your facebook app page.
facebook login php

From the settings page click on + Add Platform Button
facebook login php

Then from the select platform popup click on website icon.
facebook login php

Enter your domain name where you want to use this facebook app id & app secret. Then click on save changes button. It will ask you enter your password. After that, your changes will be saved.
facebook login php

Navigate to Facebook App Dashboard, note down App ID & App Secret. You need these in next steps where we are going to write PHP code.

3. Creating login.php

If you have followed all the above steps, you should have login.php file with session_start() PHP function and also including Facebook PHP SDK.

If you need any other informatin refer to facebook developers documentation.

Instantiate new facebook object using app id, app secret & graph version.

Use this below code to create login url using facebook app. Replace example.com with your domain url where you have created fb-callback.php file.

4. Creating fb-callback.php file

You should have fb-callback.php file with session_start() PHP function and also including Facebook PHP SDK.

Instantiate new facebook object using app id, app secret & graph version.

Getting access token and displaying errors if there are any

Get the user information using access token

Display user information in browser. If you are using this in login system, you can insert these values in database table.

Complete code of all the files

If you have any problem arranging above pieces of code, you can use this complete code.

If you have any problem, let me know using comment form below.

Incoming search terms:

  • PHP facebook login /id $fb user
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: