Techumber
Home Blog Work

Login With Facebook in PHP

Published on September 3, 2012

If you are a Web Developer or want to be a Web Developer always put yourself in end user place when you developing a web application. Most of the sites today need user registration. But Registering ourselves again and again on different sites is very boring. Every time you go to a web site and enter all details like name, email and so on. Which are already on web(hope everyone use at least one social network site).

Simple Login With Facebook in PHP Facebook is also known as virtual web. 70% of web user use face book to connect with people and share the knowledge, ideas, photos, videos, etc. So we can take advantage from facebook to create authentication for our sites. My simply clicking a single button we can get all details from facebook to register user to your site by using facebook user details. Now look at the follow step by step process to integrate facebook authentication in our php application.

Demo download

Pre-requirements

In order to work with facebook API we need to download the facebook API script. In our example we using PHP facebook API. I have included it under fblib folder. You can download compleate source code includeing fblib from above link.

Step 1:

Go to https://developers.facebook.com/apps and create an app. Simple Login With Facebook in PHPYou will get App ID and App Secret. Save them safely

Step 2:

Enter App Domains Value. Simple Login With Facebook in PHP

Step 3:

The page below enter site URL. Simple Login With Facebook in PHP

Step 4:

Create config.php and add following code to it.

define('APP_ID', 'Your appid here');
define('APP_SECRET', 'Your appsecret here');

Enter your appid,addscret values you saved in the step1.

Step 5:

Create index.php add this code.

<?php
//check if id exits in session
if (!empty($_SESSION['id'])) {
echo '<h1>Welcome '. $_SESSION['username'].'</h1>';
echo '<b>Your details<b><br /><br />';
echo 'id : ' . $_SESSION['id'];
echo '<br/>Name : ' . $_SESSION['username'];
echo '<br/>Email : ' . $_SESSION['email'];
echo '<br/><a href="logout.php">Logout</a> from demo';
}
else{
echo '<div style="text-align:center"><a href="fblogin.php"><img src="img/fb-login.png"></a> </div>';
}
?>

We will check if the id exits in session. If yes we will display details other wise we will show user the facebook login button.

Step 6

Now create fblogin.php

<?php
//Includeing facebook library and config file.
require 'fblib/facebook.php';
require 'config.php';
$fb = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $fb->getUser();
if ($user) {
try {
$userdata = $fb->api('/me');
}
catch(FacebookApiException $e){
error_log($e);
$user = null;
}
if (!empty($userdata)) {
$_SESSION['id'] = $userdata['id'];
$_SESSION['username'] = $userdata['name'];
$_SESSION['email'] = $userdata['email'];
header("Location: index.php");
}
else{
// this will kill the script if there is any error
die("There was an error.");
}
}
else{
// if not logged in you will be redirect to facebook login
$login_url = $fb->getLoginUrl(array( 'scope' => 'email'));
header("Location: " . $login_url);
}

Here, First we need to create facebook api object. Then we will use it to intract with facebook. If got the user details from facebook we will put them in the session when we accessing in index.php otherwise we will redirect the user into facebook login page.