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

Config.php

<?php

require_once 'Facebook/autoload.php';

session_start();

// Call Facebook API

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

?>

index.php

<?php
   include('config.php');

$facebook_output = '';

$facebook_helper = $facebook->getRedirectLoginHelper();

if(isset($_GET['code']))
{
 if(isset($_SESSION['access_token']))
 {
  $access_token = $_SESSION['access_token'];
 }
 else
 {
  $access_token = $facebook_helper->getAccessToken();

  $_SESSION['access_token'] = $access_token;

  $facebook->setDefaultAccessToken($_SESSION['access_token']);
 }

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

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



 if(!empty($facebook_user_info['id']))
 {
  $fbid= '$facebook_user_info['id']';
 }

 if(!empty($facebook_user_info['name']))
 {
  $name = $facebook_user_info['name'];
 }

 if(!empty($facebook_user_info['email']))
 {
  $email = $facebook_user_info['email'];
  $_SESSION['email']=$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">
   
   <head>
      <title>Login with Facebook - Adzetech</title>
      <link 
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
         rel = "stylesheet">
      <style>
         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";
		}
      </style>
   </head>
   
   <body>
 
         <?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>
            
            <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>
						
               </ul>
					
            </div>
         </div>
         
         <?php else: ?>     <!-- Before login --> 
         
         <div class = "container">
            <h1>Login with Facebook</h1>
            Not Connected
            
           <?php echo $facebook_login_url; ?>
            
            <div>
               <a href = "https://www.adzetech.com/login/"  
                  title = "Login with facebook">More information about Adzetech</a>
            </div>
         </div>
         
      <?php endif ?>
      
   </body>
</html>

Create logout.php file

<?php 
   session_start();.
   session_unset();
   
   $_SESSION['FBID'] = NULL;
   $_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   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)

// SEARCH

What are you searching for....?

// SUBSCRIBE

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

// SUBSCRIBE

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

// CATEGORIES

x
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