Login with Facebook using PHP SDK


The Facebook PHP SDK provides a server-side functionality for accessing Facebook’s server-side API calls.

Here we used PHP SDK 3.0 for the process of authentication and authorizing users for your app.

For Creating an App, which you can obtain from the App Dashboard.

In this demo module,You need App Id and App Secret for integration of the facebook login in PHP.

Live DemoLive Demo

Download Script


$appID='Your App Id';
$appSecret='Your App Secret';

Authentication & Authorization

Authentication functionality is can be achieved using the Facebook::getUser(), Facebook::getLoginUrl() and Facebook::getLogoutUrl() methods.


require 'config.php';
require 'lib/facebook/facebook.php';

$facebook = new Facebook(array(
		'appId'		=>  $appID,
		'secret'	=> $appSecret,
$user = $facebook->getUser();


		$user_profile = $facebook->api('/me');
		$params = array('next' => $base_url.'logout.php');
		$logout =$facebook->getLogoutUrl($params);
	}catch(FacebookApiException $e){
		$user = NULL;
$loginurl = $facebook->getLoginUrl(array(
				'scope'=> 'email,read_stream, publish_stream,
 user_birthday, user_location, user_work_history, user_hometown, user_photos',
				'redirect_uri'	=> $base_url.'login.php',

header('Location: '.$loginurl);
<script type="text/javascript">

Here I used one jquery plugin oauthpopup.js which is used for popup(basically here we used the customized image for facebook login link)


(function (jQuery) {
    jQuery.oauthpopup = function (options) {
        options.windowName = options.windowName || 'ConnectWithOAuth';
        options.windowOptions = options.windowOptions || 'location=0,status=0,width='+options.width+',height='+options.height+',scrollbars=1';
        options.callback = options.callback || function () {
        var that = this;
        that._oauthWindow = window.open(options.path, options.windowName, options.windowOptions);
        that._oauthInterval = window.setInterval(function () {
            if (that._oauthWindow.closed) {
        }, 1000);

And for logout of the facebook login, we used the logout.php


require 'config.php';
if(isset($_SESSION['User']) && !empty($_SESSION['User'])){
header('Location: '.$base_url);

And finally the index.php  file is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/oauthpopup.js"></script>
<script type="text/javascript">
            path: 'login.php',
            callback: function(){
if(!isset($_SESSION['User']) && empty($_SESSION['User']))   { ?>
<img src="images/facebook.png" id="facebook" style="cursor:pointer;float:left;margin-left:550px;" />
<?php  }  else{
 echo '<img src="https://graph.facebook.com/'. $_SESSION['User']['id'] .'/picture" width="30" height="30"/><div>'.$_SESSION['User']['name'].'</div>';
	echo '<a href="'.$_SESSION['logout'].'">Logout</a>';


  1. nice post….
    clear explanation, its working for me,
    thank you, keep updating.

  2. Bharath

    Good work,nice explanation

  3. Hi, I’m wondering if this would allow an application to post to their fanpage as opposed to their personal page?

  4. it is possible through the change the permissions in scope index.

  5. Bujji Babu

    Nice website, good work. thanks

  6. nice and gud work………

  7. Hallo, can you help me with setting if FB application?
    I downloaded your code, but when I want to log in, it show alert that application has a error. Or something like that.
    So problem is in my application in FB. I dont know which fileds to fill in, and which url have to type in.

    Thanks for the help.

    • Hi Tomas,

      First you need to create a Facebook App.For creating the facebook app please go through this url: http://developers.facebook.com/apps

      After creating the app you get the App Id and App Secrect.These values you need to define in config.php file. When creating the Facebook app you must define your project url in Website with Facebook Login.

      • I have created aplication, entered ID and secret code. Now:
        Website with Facebook Login – its index.php or login.php?
        Anyway after clicking login, popup window of FB to allow acces, but after that it wait around 30 seconds, and after that browser show info that there is loop of redirections.
        It seems like I cant get log in , so login.php still redirect on FB and FB back.
        And another notice, in index.php i moved sessionstart on top of the page, before html header.

        Thank for help.

  8. Hi
    I am getting error..

    Fatal error: Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:\xampp\htdocs\yy\lib\facebook\base_facebook.php:19 Stack trace: #0 C:\xampp\htdocs\yy\lib\facebook\facebook.php(18): require_once() #1 C:\xampp\htdocs\yy\login.php(7): require(‘C:\xampp\htdocs…’) #2 {main} thrown in C:\xampp\htdocs\yy\lib\facebook\base_facebook.php on line 19

    please suggest me

    • For this error you need to enable the php_curl extention in your local server wamp/xamp.

  9. gr8 gr8 gr8 .. made my day.

  10. Kate willson

    Hi.. nice work guys.. but can you guys please provide me with the code that is to store user details in own application and after login his/her account page should appear ..


  11. laly-singh-federal-way54

    Someone necessarily assist to make critically articles I might state. This is the first time I frequented your website page and thus far? I surprised with the analysis you made to create this particular put up extraordinary. Great task!

  12. Logout link is redirecting me to http://www.facebook.com. any solution?

  13. Hi iliyas,

    For logout,we generate one link like this in login.php file.
    $params = array(‘next’ => $base_url.’logout.php’);
    $logout =$facebook->getLogoutUrl($params);
    In that $params,You must define “next” argument like “http://example.com/logout.php”

  14. OviCostea

    Hi. The older API had a function getSession() with it I could check if there is a Facebook session. I used this to display a checkbox: “Post to Facebook”. My question is : How can I check now if there is a Facebook session ?

  15. manish mittal


    Thanx a lot…

    it works osm..

    now you can help me how to store its in db

    • In login.php file, $user_profile variable is contains the Facebook user data in array.if you print the $user_profile variable, you can understand the which parameters are coming from the Facebook app.Using the MySQL functions in php you can insert your table which are required data from the Facebook app.

  16. wonderful.. Thanks

  17. Bhavesh

    Thanks a lot….


  19. Hi,
    seems like I get stucked. I already made and edit at least 3 apps for this project, but without any success. Still getting: Given URL is not permitted by the application configuration.: One or more of the given URLs is not allowed by the App’s settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App’s domains.
    I’ve tried everything, finally I don’t know what’s wrong.

    • hi Peter, Facebook app must match the Website URL only. Please Disabled the sandbox mode in the Facebook app.

  20. thx alot. it’s work :)

  21. works fantastic – thanks!

Leave a Reply