How can I connect Facebook with PHP?

Login with facebook : You can use facebook login in your websites to allow users to login using their facebook account.you don’t need an extra registration and user management for your sites.you can also manage users in your facebook application page. This article explains how to integrate “Facebook login” to your websites using Facebook PHP SDK with an example and demo.

Login with facebook ( Version: 4 – Updated )

Create Facebook APP ID and APP secret .
Step 1 » Goto https://developers.facebook.com/apps/ and Click Add a New App .
» Choose Website
» Choose Name for you App and Click Create New Facebook App ID
» Choose a category for you App and click Create App ID
» Now Click Skip Quick Test

How can I connect Facebook with PHP?

Step 2 » Under settings, Provide values for App domain ( Eg:www.krizna.com ) and Contact Email and click Add Platform.
How can I connect Facebook with PHP?

Provide Values for Site URL and Mobile site URl ( Optional )
How can I connect Facebook with PHP?

Step 3 » Now under Status & Review, Click the button to make you App live .
How can I connect Facebook with PHP?

fbconfig.php file overview

Step 4 » Download the Demo package here Login with facebook .
Step 5 » Now open fbconfig.php file and enter your app ID, secret and change domain name .

// init app with app id and secret

FacebookSession::setDefaultApplication('64296382121312313','8563798aasdasdasdweqwe84');

// login helper with redirect_uri

    $helper=new FacebookRedirectLoginHelper('https://www.krizna.com/fbconfig.php');

Step 6 » Finally full code of fbconfig.php file. See the commented lines for more details

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

<?php

session_start();

// added in v4.0.0

require_once'autoload.php';

//require 'functions.php';  

useFacebookFacebookSession;

useFacebookFacebookRedirectLoginHelper;

use FacebookFacebookRequest;

useFacebookFacebookResponse;

useFacebookFacebookSDKException;

useFacebookFacebookRequestException;

useFacebookFacebookAuthorizationException;

use FacebookGraphObject;

useFacebookEntitiesAccessToken;

useFacebookHttpClientsFacebookCurlHttpClient;

useFacebookHttpClientsFacebookHttpable;

// init app with app id and secret

FacebookSession::setDefaultApplication( '64296382121312313','8563798aasdasdasdweqwe84');

// login helper with redirect_uri

    $helper=newFacebookRedirectLoginHelper('https://www.krizna.com/fbconfig.php');

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=newFacebookRequest($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;

  //checkuser($fuid,$ffname,$femail);

  header("Location: index.php");

}else{

  $loginUrl=$helper->getLoginUrl();

header("Location: ".$loginUrl);

}

?>

logout.php file overview

Logout.php file is used only to destroy facebook session and return back to your home page .
Step 7 » Enter your home page in the code to redirect after logout.

<?php

session_start();

session_unset();

    $_SESSION['FBID']=NULL;

    $_SESSION['FULLNAME'] =NULL;

    $_SESSION['EMAIL']=  NULL;

header("Location: index.php");        // you can enter home page here ( Eg : header("Location: " ."https://www.krizna.com/home.php");

?>

index.php file overview

Step 8 » You can change this file as per your need . Split this file into 2 parts before login and after login.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<?php

session_start();

?>

<!doctype html>

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

  <head>

    <title>Login withfacebook</title>

   ---------

  cssstuff

   ----------

</head>

  <body>

  <?phpif($_SESSION['FBID']):?>    

              -----------

           DisplaycontentAfteruserlogin

             ---------------  

    <?phpelse:?>    

              -----------

           Displaycontentbeforelogin

             ---------------  

    <?phpendif?>

  </body>

</html>

Finally full code of index.php file .

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<?php

session_start();

?>

<!doctype html>

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

  <head>

    <title>Login with Facebook</title>

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

</head>

  <body>

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

<div class="container">

<div class="hero-unit">

  <h2>Hello<?phpecho$_SESSION['USERNAME'];?></h2>

  <p>Welcome to"facebook login"tutorial</p>

  </div>

<div class="span4">

<ul class="nav nav-list">

<li class="nav-header">Image</li>

<li><img src="https://graph.facebook.com/<?phpecho $_SESSION['USERNAME'];?>/picture"></li>

<li class="nav-header">FacebookID</li>

<li><?php echo  $_SESSION['FBID'];?></li>

<li class="nav-header">Facebookfullname</li>

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

<div><ahref="logout.php">Logout</a></div>

</ul></div></div>

    <?phpelse:?>     <!--Before login-->

<div class="container">

<h2>Login with Facebook</h2>

           NotConnected

<div>

      <ahref="fbconfig.php">Loginwith Facebook</a></div>

      </div>

    <?phpendif?>

  </body>

</html>

That’s it . now facebook users can login into your websites using facebook login ID.

Store the User information

» You can store the user info locally . Create a mysql database and import below table structure .

CREATE TABLE IFNOTEXISTS`Users`(

  `UID`bigint(20)unsignedNOTNULL AUTO_INCREMENT,

  `Fuid`varchar(100)NOTNULL,

  `Ffname`varchar(60)NOT NULL,

  `Femail`varchar(60)DEFAULTNULL,

  PRIMARY KEY(`UID`)

);

» Open dbconfig.php file and change the DB vlaues.

<?php

define('DB_SERVER','localhost');

define('DB_USERNAME','username');    // DB username

define('DB_PASSWORD', 'password');    // DB password

define('DB_DATABASE','database');      // DB name

$connection=mysql_connect(DB_SERVER, DB_USERNAME,DB_PASSWORD)ordie("Unable to connect");

$database=mysql_select_db(DB_DATABASE)ordie("Unable to select database");

?>

» functions.php file contains a function to update the user information .

<?php

require'dbconfig.php';

functioncheckuser($fuid,$ffname,$femail){

     $check=mysql_query("select * from Users where Fuid='$fuid'");

$check=mysql_num_rows($check);

if(empty($check)){// if new user . Insert a new record

$query= "INSERT INTO Users (Fuid,Ffname,Femail) VALUES ('$fuid','$ffname','$femail')";

mysql_query($query);

}else{   // If Returned user . update the user record

$query="UPDATE Users SET Ffname='$ffname', Femail='$femail' where Fuid='$fuid'";

mysql_query($query);

}

}

?>

» Uncomment the below lines in fbconfig.php
require 'functions.php'; // Include functions
checkuser($fbid,$fbfullname,$femail); // To update local DB
That’s it .. now you can store the values locally .

Download contains all the configuration files .
Good luck

Does Facebook use PHP or hack?

Facebook doesn't use PHP for its core system, at Facebook, they uses C++ heavily on back end system.

How do I integrate Facebook login to my website?

Get Expert Social Media Marketing Training from the Pros.
Enter your website's URL..
Copy your Facebook SDK for JavaScript. This is basic code for Facebook Login. ... .
Run a test to see if anyone logged into your website via Facebook..
Customize your login button and receive a personalized code to insert into your website..

How do I log into Facebook using laravel?

Laravel 9 Socialite Login with Facebook Account Example.
Step 1: Install Laravel 9. ... .
Step 2: Install JetStream. ... .
Step 3: Install Socialite. ... .
Step 4: Create Facebook App. ... .
Step 5: Add Database Column. ... .
Step 6: Create Routes. ... .
Step 7: Create Controller. ... .
Step 8: Update Blade File..

How do I find the source code on Facebook?

Using Either.
Open either browser and navigate to the Facebook page you want to view..
Right-click on the page..
Slide down to "View Page Source" or "View Source" and click..