How To Login With Facebook In Php With Example


today in this tutorial we learn how to log in with Facebook in PHP. We can use Facebook SDK for PHP login to allow the users to get access to the websites. This tutorial will explain to 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] - PhpCodingStuff
Login With Facebook
  • Need to go https://developers.facebook.com/apps/ and click on add a new group button to make the app ID.
  • Choose Website
  • Give an app name and click on
  • Create New Facebook App ID
  • Click on Create App ID
  • Click on Skip Quick Test

On Final stage, it will show as below shown image.

Login With Facebook Using PHP [Step By Step] - PhpCodingStuff
Fbconfig.php File Overview

Now open the fbconfig.php file and add your app ID and app Secret. this tutorial facebook SDK PHP.

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

Finally, fbconfig.php file as shown below code how to login with Facebook  −

<?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);
   }
?>
Login Page Overview

This step Login page is used to login into Facebook in PHP.

<?php
   session_start();
   session_unset();
   
   $_SESSION['FBID'] = NULL;
   $_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>
Index.php

The index page is shown below.

<?php
   session_start(); 
?>
<html xmlns:fb = "http://www.facebook.com/2008/fbml">
   
   <head>
      <title>How To Login With Facebook In Php With Example - Phpcodingstuff.com</title>
      <link 
         href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel = "stylesheet">
   </head>
   
   <body>
      <?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 phpcodingstuff.com</p>
            </div>
            
            <div class = "span4">
				
               <ul class = "nav nav-list">
                  <li class = "nav-header"><img src="https://i.imgur.com/76Chgd4.png"></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>
						
               </ul>
					
            </div>
         </div>
         
         <?php else: ?>     <!-- Before login --> 
         
         <div class = "container">
            <h1>Login with Facebook</h1>
                      
            <div>
               <a href = "fbconfig.php"><img src="https://www.phpcodingstuff.com/uploads/tutorial_images/how_to_login_facebook_login_in_php.png"></a>
            </div>
         </div>
         
      <?php endif ?>
      
   </body>
</html>
Logout Facebook

This step Below code is used to logout Facebook. we learn Facebook login integration in PHP.

<?php 
   session_start();.
   session_unset();
   
   $_SESSION['FBID'] = NULL;
   $_SESSION['FULLNAME'] = NULL;
   $_SESSION['EMAIL'] =  NULL;
   header("Location: index.php");        
?>

It will produce the result. Before trying this example, please log out of your Facebook account in your browser.

Login With Facebook Using PHP [Step By Step] - PhpCodingStuff

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close