Implementing Login With Facebook In PHP

Read {count} times since 2020

In the last post, I introduced you to a PHP OAuth Class named OAuth API. In this post I’m going to tell you how to implement Login With Facebook feature in PHP using the class I introduced. It’s very simple. I will tell the ways of implementing step by step. This tutorial includes placing a Login with Facebook button on web site to authorization and finishes when we get user details. You should read the previous post before starting this tutorial.


First of all, we will design our Login With Facebook button. Then we will move on to the server side. We are going to design the button using HTMLCSS and a small icon.

Place the below HTML code on the place where you want to show the button :

Login With Facebook

You should need a Facebook small logo for making the button attractive. I have included the image below :


Here is the CSS code that will decorate the button. I have provided the image URL as


display: inline-block;

height: 43px;

margin: 0px;

padding: 0px 20px 0px 52px;

font-family: ‘Ubuntu’, sans-serif;

font-size: 18px;

font-weight: 400;

color: #fff;

line-height: 41px;

background: #3b579d url( no-repeat 14px 8px scroll;

-webkit-border-radius: 4px;

-moz-border-radius: 4px;

border-radius: 4px;

text-decoration: none;



The finished button will look something like below :

The finished button

Server Side


This is the file that handles the authorization. You need the oauth_client.php which we mentioned in the previous post. You need a client id and client secret which you get when you create an app @ (Tutorial here).

Here is the complete code :




$client = new oauth_client_class;

$client->server = ‘Facebook’;

$client->debug = false;

$client->debug_http = true;

$client->redirect_uri = ‘http://’.$_SERVER[‘HTTP_HOST’].dirname(strtok($_SERVER[‘REQUEST_URI’],’?’)).’/login_with_facebook.php’;

$client->client_id = ";/*Client ID*/

$client->client_secret = ";/*Client Secret*/

$client->scope = ’email,user_about_me,user_birthday’;

$application_line = \__LINE__;

if(strlen($client->client_id) == 0 || strlen($client->client_secret) == 0){

 die(‘Please go to Facebook Apps page , &#8216;.

  ‘create an application, and in the line ‘.$application_line.

  ‘ set the client_id to App ID/API Key and client_secret with App Secret’);


if(($success = $client->Initialize())){

 if(($success = $client->Process())){


   $client->error = $client->authorization_error;

   $success = false;


   $success = $client->CallAPI(‘;, &#8216;GET&#8217;, array(), array(&#8216;FailOnAccessError&#8217;=>true), $user);





    $birthday=date(‘d/m/Y’, strtotime($user->birthday));






 $success = $client->Finalize($success);



 die("Something Happened","redirect_uri."‘>Try Again");




 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">







OAuth client error


Error: <?php echo HtmlSpecialChars($client->error); ?>




Don’t forget to replace client_idclient_secret values.

How It Works

  1. When a user clicks Login With Facebook button, the user will be redirected to the login_with_facebook.php page.
  2. The PHP file will redirect the user to Facebook‘s authorization page with all the app details needed by Facebook.
  3. When the user authorizes the app, he/she will be redirected back to login_with_facebook.php page with a GET parameter named code.
  4. OAuth API library will request an access token to Facebook with the given code.
  5. When it receives the access token, It sends a request to with the access_token and the URL will return the details of the user.
  6. It also sends a request to with the access token and the image size needed (200 x 200).


$email – The E-Mail of the user

$name – The Name of the user

$gender – The Gender of the user

$birthday – The Birthday of the user

$image – The Image URL of the user

Isn’t this tutorial simple ?

If you encountered any problems or have a suggestion, please say it on the comments. I will be happy to respond.

Show Comments