Login with facebook SDK using PHP

Nowadays web users are not interested in filling out a broad form on the website for registration. Quick enrollment process helps get more subscribers to your website. Logging on to Facebook is a quick and powerful way to integrate the website registration and login system. Facebook is the most popular social network and most people have an account with Facebook. Sign in to Facebook

Login with facebook SDK using PHP and MYSQL

PHP SDK enables web application access to the Facebook API. The Login with Facebook account can be easily implemented using Facebook SDK for PHP. This tutorial demonstrates how to implement Facebook user login and registration system using PHP, and how to store user profile data in the MySQL database.

Our example Facebook Login script is using Facebook PHP SDK v5 with Facebook Graph API to create a PHP and MySQL Facebook Login Framework.

To get started with the new Facebook SDK v5.x update make sure that your device meets the following requirements.

  • PHP version should be 5.4 or greater.
  • The mbstring extension should be enabled.

Create Facebook App

You need to create a Facebook App to access the Facebook API and define the App ID & App Secret when calling the Facebook API. Follow the step-by-step guide for developing Facebook App and producing App ID & Secret in the Facebook Developers Dashboard.

  • Go to the Facebook for Developers page and log in with your Facebook account.
  • Click the My Apps link at the top navigation bar and select Add New App.
    • Enter the Display Name and Contact Email.
    • Click on the Create App ID button.
    • You will be redirected to the App Dashboard.
  • Navigate to the Settings » Basic page.
    • Specify the App Domains and select the Category of your App.
    • Click the Save Changes.
  • Navigate to the Add a Product page by clicking the PRODUCTS(+) link at the left navigation menu panel.
    • Select Facebook Login to Set Up.
    • Select Web as the App platform.
    • Enter the Site URL and Save.
  • Navigate to the Facebook Login » Settings page.
    • In the Valid OAuth Redirect URIs field, enter the Redirect URL.
    • Click the Save Changes.

Go to Settings tab » Basic tab, take note of your aplication id as App ID and Secret code. This App ID and Secret code gives you to access the Facebook APIs.

Login with facebook SDK using PHP and MYSQL

Login with facebook SDK using PHP and MYSQL

Include Facebook SDK for PHP



require_once 'Facebook/autoload.php';


// Call Facebook API

$facebook = new FacebookFacebook([
  'app_id'      => 'YOUR-APP-ID',
  'app_secret'     => 'YOUR-APP-SECRET-CODE',
  'default_graph_version'  => 'v5.0'




$facebook_output = '';

$facebook_helper = $facebook->getRedirectLoginHelper();

  $access_token = $_SESSION['access_token'];
  $access_token = $facebook_helper->getAccessToken();

  $_SESSION['access_token'] = $access_token;


 $graph_response = $facebook->get("/me?fields=name,email", $access_token);

 $facebook_user_info = $graph_response->getGraphUser();
  $img = 'http://graph.facebook.com/'.$facebook_user_info['id'].'/picture';

  $fbid= '$facebook_user_info['id']';

  $name = $facebook_user_info['name'];

  $email = $facebook_user_info['email'];

// Get login url
    $facebook_permissions = ['email']; // Optional permissions
    $facebook_login_url = $facebook_helper->getLoginUrl('https://adzetech.com/login/index.php', $facebook_permissions);
    // Render Facebook login button

   $facebook_login_url = '<div align="center" style="margin-bottom: 15px;"><a href="'.$facebook_login_url.'" class="fb connect">Sign in with Facebook</a></div>';

<html xmlns:fb = "http://www.facebook.com/2008/fbml">
      <title>Login with Facebook - Adzetech</title>
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
         rel = "stylesheet">
         a.fb {
			font-family: Lucida Grande, Helvetica Neue, Helvetica, Arial, sans-serif;
			display: inline-block;
			font-size: 14px;
			padding: 13px 30px 15px 44px;
			background: #3A5A97;
			color: #fff;
			text-shadow: 0 -1px 0 rgba(0,0,20,.4);
			text-decoration: none;
			line-height: 1;
			position: relative;
			border-radius: 5px;

		.connect:before {
			display: inline-block;
			position: relative;
			background-image: url(https://adzetech.com/images/uploads/facebook.png);
			height: 23px;
			background-repeat: no-repeat;
			background-position: 0px 3px;
			text-indent: -9999px;
			text-align: center;
			width: 29px;
			line-height: 23px;
			margin: -8px 7px -7px -30px;
			padding: 2 25px 0 0;
			content: "f";
         <?php if ($_SESSION['email']): ?>      <!--  After user login  -->
         <div class = "container">
            <div class = "hero-unit">
               <h1>Hello <?php echo $name; ?></h1>
               <p>Welcome to "facebook login" tutorial</p>
            <div class = "span4">
               <ul class = "nav nav-list">
                  <li class = "nav-header">Image</li>
                  <li><img src = "<?php echo $img; ?>"></li>
                  <li class = "nav-header">Facebook ID</li>
                  <li><?php echo  $fbid; ?></li>
                  <li class = "nav-header">Facebook fullname</li>
                  <li><?php echo name; ?></li>
                  <li class = "nav-header">Facebook Email</li>
                  <li><?php echo $email; ?></li>
                  <div><a href="logout.php">Logout</a></div>
         <?php else: ?>     <!-- Before login --> 
         <div class = "container">
            <h1>Login with Facebook</h1>
            Not Connected
           <?php echo $facebook_login_url; ?>
               <a href = "https://www.adzetech.com/login/"  
                  title = "Login with facebook">More information about Adzetech</a>
      <?php endif ?>

Create logout.php file

   header("Location: index.php");        

In this tutorial, we've tried to make Facebook Login implementation quicker and easier. The sample code incorporates Facebook Login into PHP's Facebook SDK.. You need to add the SDK library files separately, our source code contains all the required files with the SDK v5 for PHP. Only define some minimal settings to add Facebook login framework to your website using PHP.

In this tutorial we tried to make implementation of Facebook Login faster and easier. The sample code incorporates Facebook Login into PHP's Facebook SDK. You need to add the SDK library files separately, our source code contains all the files needed for PHP using the SDK v5. You just have to define a certain minimum set

Check out these related:

Leave comment

Comments (1)


What are you searching for....?


Subscribe to us on youtube for updates of our videos first on youtube.


To receive updates on the latest news, tutorials and special offers, subscribe to our email newsletter today!


We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine