We now have a youtube channel. Subscribe!

How to Implement Login using Facebook in PHP

How to Implement Login using Facebook in PHP

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about how to Implement Login via Facebook in PHP.

We can use facebook login to allow users have access to the site. This tutorial guide gives an in-depth explanation about login with facebook PHP SDK.

Login with Facebook(Steps)

  • Choose website.
  • Give an app name and click on the Create New Facebook App ID.
  • Click on Create app ID.
  • Click on Skip Quick Test.

On the final stage, it will display as the image below -

fbconfig.php File Overview

  • Now open fbconfig.php file and add your app ID and the app Secrete.

FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' );
// login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('You web address' );

Finally fbconfig.php file as seen below -

   // 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( '496544657159182','e6d239655aeb3e496e52fabeaf1b1f93' );
   // login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper('https://www.webdesigntutorialz.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);

Login Page Overview

Login page is used to log into Facebook -

   header("Location: index.php");        


The index.php page is as shown below -

<html xmlns:fb = "http://www.facebook.com/2008/fbml">
      <title>Login with Facebook</title>
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" 
         rel = "stylesheet">
      <?php if ($_SESSION['FBID']): ?>      <!--  After user login  -->
         <div class = "container">
            <div class = "hero-unit">
               <h1>Hello <?php echo $_SESSION['USERNAME']; ?></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 = "https://graph.facebook.com/<?php 
                     echo $_SESSION['FBID']; ?>/picture"></li>
                  <li class = "nav-header">Facebook ID</li>
                  <li><?php echo  $_SESSION['FBID']; ?></li>
                  <li class = "nav-header">Facebook fullname</li>
                  <li><?php echo $_SESSION['FULLNAME']; ?></li>
                  <li class = "nav-header">Facebook Email</li>
                  <li><?php echo $_SESSION['EMAIL']; ?></li>
                  <div><a href="logout.php">Logout</a></div>
         <?php else: ?>     <!-- Before login --> 
         <div class = "container">
            <h1>Login with Facebook</h1>
            Not Connected
               <a href = "fbconfig.php">Login with Facebook</a>
               <a href = "http://www.webdesigntutorialz.com"  
                  title = "Login with facebook">More information about Webdesigntutorialz</a>
      <?php endif ?>


When the above code is implemented, it will produce the following result. Before you try this example, kindly logout your facebook account in your web browser -

Logout Facebook

The following code below is used to logout Facebook -

   header("Location: index.php");        

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about PHP Paypal integration.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain