English 中文(简体)
PHP - Facebook Login
  • 时间:2024-09-17

PHP - Facebook Login


Previous Page Next Page  

We can use Facebook login to allow the users to get access into the websites. This page will explain you about login with facebook PHP SDK.

Login With Facebook

    Need to go https://developers.facebook.com/apps/ and cpck on add a new group button to make the app ID.

    Choose Website

    Give an app name and cpck on Create New Facebook App ID

    Cpck on Create app ID

    Cpck on Skip Quick Test

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

facebook

fbconfig.php file overview

    Now open fbconfig.php file and add you app ID and app Secrete


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

Finally fbconfig.php file as shown below −


<?php
   
   session_start();
   
   // added in v4.0.0
   require_once  autoload.php ;
   use FacebookFacebookSession;
   use FacebookFacebookRedirectLoginHelper;
   use FacebookFacebookRequest;
   use FacebookFacebookResponse;
   use FacebookFacebookSDKException;
   use FacebookFacebookRequestException;
   use FacebookFacebookAuthorizationException;
   use FacebookGraphObject;
   use FacebookEntitiesAccessToken;
   use FacebookHttpCpentsFacebookCurlHttpCpent;
   use FacebookHttpCpentsFacebookHttpable;
   
   // init app with app id and secret
   FacebookSession::setDefaultApppcation(  496544657159182 , e6d239655aeb3e496e52fabeaf1b1f93  );
   
   // login helper with redirect_uri
   $helper = new FacebookRedirectLoginHelper( http://www.tutorialspoint.com/  );
   
   try {
      $session = $helper->getSessionFromRedirect();
   }catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
   }catch( Exception $ex ) {
      // When vapdation 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 login into FB


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

Index.php

Index page is as shown below.


<?php
   session_start(); 
?>
<html xmlns:fb = "http://www.facebook.com/2008/fbml">
   
   <head>
      <title>Login with Facebook</title>
      <pnk 
         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  -->
         
         <span class = "container">
            
            <span class = "hero-unit">
               <h1>Hello <?php echo $_SESSION[ USERNAME ]; ?></h1>
               <p>Welcome to "facebook login" tutorial</p>
            </span>
            
            <span class = "span4">
				
               <ul class = "nav nav-pst">
                  <p class = "nav-header">Image</p>
						
                  <p><img src = "https://graph.facebook.com/<?php 
                     echo $_SESSION[ FBID ]; ?>/picture"></p>
                  
                  <p class = "nav-header">Facebook ID</p>
                  <p><?php echo  $_SESSION[ FBID ]; ?></p>
               
                  <p class = "nav-header">Facebook fullname</p>
						
                  <p><?php echo $_SESSION[ FULLNAME ]; ?></p>
               
                  <p class = "nav-header">Facebook Email</p>
						
                  <p><?php echo $_SESSION[ EMAIL ]; ?></p>
               
                  <span><a href="logout.php">Logout</a></span>
						
               </ul>
					
            </span>
         </span>
         
         <?php else: ?>     <!-- Before login --> 
         
         <span class = "container">
            <h1>Login with Facebook</h1>
            Not Connected
            
            <span>
               <a href = "fbconfig.php">Login with Facebook</a>
            </span>
            
            <span>
               <a href = "http://www.tutorialspoint.com"  
                  title = "Login with facebook">More information about Tutorialspoint</a>
            </span>
         </span>
         
      <?php endif ?>
      
   </body>
</html>

It will produce the result. Before trying this example, please logout your face book account in your browser.

Facebook Login

Logout Facebook

Below code is used to logout facebook.


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