Welcome To Facebook Log I

                                                   Welcome To Facebook Log
Welcome To Facebook Log I

PHP - Facebook Login

We can use Facebook login to allow the users to get access into the websites. This page will explain you about login with facebook PHP SDK.
Login With Facebook

Need to go https://developers.facebook.com/apps/ and click on add a new group button to make the app ID.

Give an app name and click on Create New Facebook App ID
Click on Create app ID
Click on Skip Quick Test

On Final stage, it will show as below shown image.

fbconfig.php file overview

Now download zip from here

Now open fbconfig.php file and add you app ID and app Secrete

FacebookSession::setDefaultApplication( 'your app ID','App Secrete ' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('You web address' );

Finally fbconfig.php file as shown below −

// added in v4.0.0
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;

// init app with app id and secret
FacebookSession::setDefaultApplication('496544657159182','e6d239655aeb3e496e52fabeaf1b1f93' );
// login helper with redirect_uri
$helper = newFacebookRedirectLoginHelper('http://www.tutorialspoint.com/' );
try {
$session = $helper->getSessionFromRedirect();
}catch( FacebookRequestException $ex ) {
// When Facebook returns an error
}catch( Exception $ex ) {
// When validation fails or other local issues

// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();

// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID

/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;

/* ---- header location after session ----*/
header("Location: index.php");
}else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
Login page Overview

Login page is used to login into FB


header("Location: index.php");

Index page is as shown below.

xmlns:fb = "http://www.facebook.com/2008/fbml"

Login with Facebook

href = "http://www.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css"
rel = "stylesheet"

<?php if ($_SESSION['FBID']): ?> <!-- After user login -->

class = "container"

class = "hero-unit"
Hello <?php echo $_SESSION['USERNAME']; ?>
Welcome to "facebook login" tutorial

class = "span4"

class = "nav nav-list"
class = "nav-header"Image

<img src = "https://graph.facebook.com/<?php
echo $_SESSION['FBID']; ?>/picture">

class = "nav-header"Facebook ID
<?php echo $_SESSION['FBID']; ?>

class = "nav-header"Facebook fullname

<?php echo $_SESSION['FULLNAME']; ?>

class = "nav-header"Facebook Email

<?php echo $_SESSION['EMAIL']; ?>


<?php else: ?> <!-- Before login -->

class = "container"
Login with Facebook
Not Connected

href = "fbconfig.php"Login with Facebook

href = "http://www.tutorialspoint.com"
title = "Login with facebook"More information about Tutorialspoint

<?php endif ?>

It will produce the result. Before trying this example, please logout your face book account in your browser.
Welcome To Facebook Log I Welcome To Facebook Log I Reviewed by Efukikata Efet on 13:16 Rating: 5

No comments