Here we learn how to Login with facebook using PHP.

Now a days Facebook have become large social network in the world. You can use your facebook account to login into other sites. You can easily use your facebook login in your websites to allow users to login by using their facebook accounts. You do not need any extra registration or management. By using this article explain how to Login with facebook using PHP.Behind this article we have reason to facilitate our user to create the same functionality with their blog , because now a days its a good trend to increase popularity of a website , its not an issue whether you are using simple php sites or wordpress themes you can integrate this functionality in any type of website.



Login with facebook ( Version: 4 – Updated )

You May Also Like:

Create Facebook APP ID.
Step 1 

  • First you click on this link and Click Add a New App .
  • Choose Website
  • Choose Name for you App and Click Create New Facebook App ID
  • Choose a category for you App and click Create App ID
  • Now Click Skip Quick Test

Step 2 

  • Under settings, Provide values for App domain ( ) and Contact Email and click Add Platform.

Step 3 

  • Now under Status & Review, Click the button to make you App live .

Step 4

  • Now open fbconfig.php file and enter your app ID, secret and change domain name .
// init app with app id and secret
FacebookSession::setDefaultApplication( '64296382121312313','8563798aasdasdasdweqwe84' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('' );

Step 5

  • Finally full code of fbconfig.php file.
// added in v4.0.0
require_once 'autoload.php';
//require 'functions.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( '64296382121312313','8563798aasdasdasdweqwe84' );
// login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper('' );
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: index.php");
} else {
  $loginUrl = $helper->getLoginUrl();
 header("Location: ".$loginUrl);

logout.php file overview

Logout.php file is used only to destroy facebook session and return back to your home page .

Step 6

  • Enter your home page in the code to redirect after logout.
header("Location: index.php");        // you can enter home page here ( Eg : header("Location: " .""); 

index.php file overview

Step 7

  • You can change this file as per your need . Split this file into 2 parts before login and after login.
<!doctype html>
<html xmlns:fb="">
    <title>Login with facebook</title>
   --- --- --- 
  css stuff
   --- --- ----
  <?php if ($_SESSION['FBID']): ?>      
              -- --- - - - -- - 
           Display content After user login
             -- -- - --- ---- -- -  
    <?php else: ?>     
              -- --- - - - -- - 
           Display content before login
             -- -- - --- ---- -- -  
    <?php endif ?>

Finally full code of index.php file .

<!doctype html>
<html xmlns:fb="">
    <title>Login with Facebook</title>
<link href="" 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="<?php echo $_SESSION['USERNAME']; ?>/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>
<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></div>
    <?php endif ?>


Store the User information

You can store the user info locally . Create a mysql database and import below table structure .

  `UID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `Fuid` varchar(100) NOT NULL,
  `Ffname` varchar(60) NOT NULL,
  `Femail` varchar(60) DEFAULT NULL,

Open dbconfig.php file and change the DB vlaues.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');    // DB username
define('DB_PASSWORD', 'password');    // DB password
define('DB_DATABASE', 'database');      // DB name
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die( "Unable to connect");
$database = mysql_select_db(DB_DATABASE) or die( "Unable to select database");

functions.php file contains a function to update the user information .

	require 'dbconfig.php';
	function checkuser($fuid,$ffname,$femail){
    	$check = mysql_query("select * from Users where Fuid='$fuid'");
		$check = mysql_num_rows($check);
		if (empty($check)) { // if new user . Insert a new record		
		$query = "INSERT INTO Users (Fuid,Ffname,Femail) VALUES ('$fuid','$ffname','$femail')";
		} else {   // If Returned user . update the user record		
		$query = "UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";

About The Author

Related Posts

2 Responses

Leave a Reply

Your email address will not be published.