In this article, we will explain to you how to integrate login with Facebook using PHP. Here we will create a system where the user can log in with his Facebook account on your website. So, in this post, we share a tutorial on how to login or sign in or sign up with a Facebook account in your PHP web application.
To login with Facebook in PHP, Facebook PHP SDK has provided Facebook OAuth API, which is very easy and useful to implement login using a Facebook account on your website. The Facebook login product provides rights to the user to login to the website using their Facebook account credential without registering on that particular website.
In this example, we will use the Facebook PHP SDK library, this library gives users permission to sign in with their existing Facebook accounts on your website. Below, you can find how to integrate Facebook login API into your PHP website and you can also find the process of how to get Facebook API id and how to integrate the Facebook SDK library into your existing PHP library to login.
Now we need to app id facebook and App Secret for Facebook login. first we have to create the Facebook app. so you can see below steps.

After we got the Facebook app id and Secret key, now we need to download the Facebook Sdk for PHP library. For this, we need to go to the command prompt and first type the command composer, after that, we need to run the following command.
composer require facebook/graph-sdk
config.php
now, first, we need to create a config.php file. after that, we will configure the Facebook app id and secret key and add the Facebook SDK library in this file.
<?php
require_once 'vendor/autoload.php';
if (!session_id())
{
session_start();
}
$facebook = new \Facebook\Facebook([
'app_id' => 'enter facebook app id',
'app_secret' => 'enter facebook app secret key',
'default_graph_version' => 'v2.10'
]);
?>
index.php
<?php
include('config.php');
$facebook_output = '';
$facebook_helper = $facebook->getRedirectLoginHelper();
if(isset($_GET['code']))
{
if(isset($_SESSION['access_token']))
{
$access_token = $_SESSION['access_token'];
}
else
{
$access_token = $facebook_helper->getAccessToken();
$_SESSION['access_token'] = $access_token;
$facebook->setDefaultAccessToken($_SESSION['access_token']);
}
$_SESSION['user_id'] = '';
$_SESSION['user_name'] = '';
$_SESSION['user_email_address'] = '';
$_SESSION['user_image'] = '';
$graph_response = $facebook->get("/me?fields=name,email", $access_token);
$facebook_user_info = $graph_response->getGraphUser();
if(!empty($facebook_user_info['id']))
{
$_SESSION['user_image'] = 'http://graph.facebook.com/'.$facebook_user_info['id'].'/picture';
}
if(!empty($facebook_user_info['name']))
{
$_SESSION['user_name'] = $facebook_user_info['name'];
}
if(!empty($facebook_user_info['email']))
{
$_SESSION['user_email_address'] = $facebook_user_info['email'];
}
}
else
{
$facebook_permissions = ['email'];
$facebook_login_url = $facebook_helper->getLoginUrl('https://xpertphp.com/demos/php/php_facebook_login/', $facebook_permissions);
$facebook_login_url = '<div align="center"><a href="'.$facebook_login_url.'"><img src="facebook-login-button.png" /></a></div>';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook Account using PHP</title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">Login with Facebook Account using PHP</h2>
<br />
<div class="panel panel-default">
<?php
if(isset($facebook_login_url))
{
echo $facebook_login_url;
}
else
{
echo '<div class="panel-heading">Welcome User</div><div class="panel-body">';
echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />';
echo '<h3><b>Name :</b> '.$_SESSION['user_name'].'</h3>';
echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>';
echo '<h3><a href="logout.php">Logout</h3></div>';
}
?>
</div>
</div>
</body>
</html>
delete.php
<?php
// destroy all session
session_destroy();
header('location:index.php');
?>