Facebook Login Application in PHP

Introduction

This article explains how to create a Facebook login application for your website. For this application you first need the Facebook PHP SDK master. You can download it from http://www.github.com/. After downloading this application you will put the package in your PHP  "c/wamp/www" directory. You will then create your apps Id and secret Id. For creating the apps id and secret id you will go to the Facebook Developer's site and click on Apps then click on the create new apps and follow the next step as in the following.

Step 1

fblogin2.jpg

Step 2

fblogin3.jpg

Step 3

fblogin4.jpg

Step 4

fblogin5.jpg

After completing those steps, create an application. You can use this application to easily login, making it easy to connect with people using your app. The Facebook SDK for JavaScript provides a straightforward path to group action login together with your internet sites and mobile web apps.

Example

<?php session_start();

require 'facebook-php-sdk-master/src/facebook.php';

$fb= new Facebook(array(

  'appId'  => 'AppId',

  'secret' => 'SecretId',

 ));

$Usr = $fb->getUser();

if ($Usr)

{

  try {

    $Usr_Profile = $fb->api('/me');

  } catch (FacebookApiException $e) {

    $Usr = null;

  }

}

?>

<html>

  <body>

   <?php

    if ($Usr) {

    foreach($Usr_Profile as $skey => $sval)

    {

        $_SESSION["$skey"]=$sval;  

    }  

    $_SESSION['fb_name']=$Usr_Profile['name'];

    $_SESSION['fb_id']=$Usr_Profile['id'];

    $logoutUrl = $fb->getLogoutUrl();

    $_SESSION['fb_logout']= $logoutUrl;

     ?>

      <pre>           

        <?php //print htmlspecialchars(print_r($Usr_Profile, true)) ?>

      </pre>

    <?php } else { ?>

      <fb:login-button></fb:login-button>

    <?php } ?>

    <div id="fb-root"></div>

    <script>

        window.fbAsyncInit = function () {

            FB.init({

                appId: '<?php echo $fb->getAppID() ?>',

                cookie: true,

                xfbml: true,

                oauth: true

            });

            FB.Event.subscribe('auth.login', function (response) {

                window.location.href = "index.php";

            });

            FB.Event.subscribe('auth.logout', function (response) {

                window.location.reload();

            });

        };

        (function () {

            var e = document.createElement('script'); e.async = true;

            e.src = document.location.protocol +

          '//connect.facebook.net/en_US/all.js';

            document.getElementById('fb-root').appendChild(e);

        } ());

    </script>

  </body>

</html>
 

Output

fblogin.jpg
fblogin1.jpg