Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development In Your Email

Login With Facebook Using PHP

Thursday, Jan 19, 2017

Tags:- PHP


In this tutorial we will show how to login with facebook using PHP and facebook Graph API.In this way you can integrate facebook in your website or you can also create a web app using this method.You may also like login with google using PHP.


Login With Facebook Using PHP


To Login With Facebook Using PHP It Takes Only Two Steps:-

  1. Create a facebook app and get all the details
  2. Make a PHP file to login with facebook


Step 1.Create a facebook app and get all the details

Create a facebook app here https://developers.facebook.com/apps then get your facebook app id and facebook app secret and then edit configure.php file and insert your details and callback url in respective variable you will find that file in src folder.You may also like login with twitter using PHP.



Step 2.Make a PHP file to login with facebook

We make a PHP file and save it with a name fb_login.php

<?php
require 'src/config.php';
require 'src/facebook.php';

$facebook = new Facebook(array(
 'appId'  => $config['App_ID'],
 'secret' => $config['App_Secret'],
 'cookie' => true
));

if(isset($_GET['fbTrue']))
{
 $token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url']). "&client_secret=".$config['App_Secret']."&code=" . $_GET['code']; 
 $response = file_get_contents($token_url);
 $params = null;
 parse_str($response, $params);
 $graph_url = "https://graph.facebook.com/me?access_token=" .$params['access_token'];
 $user = json_decode(file_get_contents($graph_url));
 $content = $user;
}
else
{
 $content = '<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,user_likes,publish_stream"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>';
}

include('html.inc');
?>

<html>
<body>

<?php
$config['callback_url'] = "http://www.your_site.com/index.php/?fbTrue=true";
?>

<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,user_likes,publish_stream"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>

</body>
</html>

In this step we add all the required files for facebook login and then create a facebook array and insert all the required variables in facebook graph url provided by facebook and finally get the user details.You may also like login with linkedin using PHP.



Thats all, this is how to do login with facebook using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email