Login with Facebook using PHP [Step by Step] - PhpCodingStuff

Login with Facebook using PHP [Step by Step] - PhpCodingStuff

How to login with facebook in php. We can use Facebook login to allow the users to get access into the websites. In this tutorial will explain you about login with facebook PHP SDK.

We need to Fackbook login into many websites, it does not bother the user much, he login easily on the website and by login, we get all the data of the user like name email id age, etc. so in this tutorial, we learn Facebook login integration in PHP.

Login with Facebook using PHP [Step by Step]

<?php

   session_start();

   // added in v4.0.0
   require_once 'autoload.php';
   use Facebook\FacebookSession;
   use Facebook\FacebookRedirectLoginHelper;
   use Facebook\FacebookRequest;
   use Facebook\FacebookResponse;
   use Facebook\FacebookSDKException;
   use Facebook\FacebookRequestException;
   use Facebook\FacebookAuthorizationException;
   use Facebook\GraphObject;
   use Facebook\Entities\AccessToken;
   use Facebook\HttpClients\FacebookCurlHttpClient;
   use Facebook\HttpClients\FacebookHttpable;

   // init app with app id and secret
   FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' );

   // login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('http://www.phpcodingstuff.com/' );

   try {
      $session = $helper->getSessionFromRedirect();
   }catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
   }catch( Exception $ex ) {
      // When validation fails or other local issues
   }

   // see if we have a session
   if ( isset( $session ) ) {
      // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();

      // get response
      $graphObject = $response->getGraphObject();
      $fbid = $graphObject->getProperty('id');           // To Get Facebook ID
      $fbfullname = $graphObject->getProperty('name');   // To Get Facebook full name
      $femail = $graphObject->getProperty('email');      // To Get Facebook email ID

      /* ---- Session Variables -----*/
      $_SESSION['FBID'] = $fbid;
      $_SESSION['FULLNAME'] = $fbfullname;
      $_SESSION['EMAIL'] =  $femail;

      /* ---- header location after session ----*/
      header("Location: index.php");
   }else {
      $loginUrl = $helper->getLoginUrl();
      header("Location: ".$loginUrl);
   }
?>

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Login Page in PHP with Database Source Code

Simple login form in PHP using session. Here you'll learn how to create complete login form and authenticate users from MySQL database in PHP.

How to use highchart in php with example

Here, i will let you know that how to use highchart in php application with example. Highchart provides feature to draw different type of charts.

Country State City Dropdown list in PHP MySQL PHP

Country state city dropdown using ajax in php. You'll learn how to populate country city state dropdown based on previous selection in php using ajax

How to Store Form Data in Database using PHP -2020

in PHP Insert Data Into MySQL Database From Form. Here you will learn how you can insert or store or save form data into mysql database table using PHP code

Facebook Login With PHP Laravel Socialite

We will implement a Facebook login with the PHP Laravel Socialite package. This package is really awesome for all social logins.