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
Step 2 » Under settings, Provide values for App domain ( Eg:www.krizna.com ) and Contact Email and click Add Platform.
Provide Values for Site URL and Mobile site URl ( Optional )
Step 3 » Now under Status & Review, Click the button to make you App live .
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
<?php session_start(); // added in v4.0.0 require_once 'autoload.php'; //require 'functions.php'; use FacebookFacebookSession; use FacebookFacebookRedirectLoginHelper; use FacebookFacebookRequest; use FacebookFacebookResponse; use FacebookFacebookSDKException; use FacebookFacebookRequestException; use FacebookFacebookAuthorizationException; use FacebookGraphObject; use FacebookEntitiesAccessToken; use FacebookHttpClientsFacebookCurlHttpClient; use FacebookHttpClientsFacebookHttpable; // 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' ); 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; //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.
<?php session_start(); ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>Login with facebook</title> --- --- --- css stuff --- --- ---- </head> <body> <?php if ($_SESSION['FBID']): ?> -- --- - - - -- - Display content After user login -- -- - --- ---- -- - <?php else: ?> -- --- - - - -- - Display content before login -- -- - --- ---- -- - <?php endif ?> </body> </html>
Finally full code of index.php file .
<?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> <?php if ($_SESSION['FBID']): ?> <!-- After user login --> <div class="container"> <div class="hero-unit"> <h1>Hello <?php echo $_SESSION['USERNAME']; ?></h1> <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/<?php echo $_SESSION['USERNAME']; ?>/picture"></li> <li class="nav-header">Facebook ID</li> <li><?php echo $_SESSION['FBID']; ?></li> <li class="nav-header">Facebook fullname</li> <li><?php echo $_SESSION['FULLNAME']; ?></li> <div><a href="logout.php">Logout</a></div> </ul></div></div> <?php else: ?> <!-- Before login --> <div class="container"> <h1>Login with Facebook</h1> Not Connected <div> <a href="fbconfig.php">Login with Facebook</a></div> </div> <?php endif ?> </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 IF NOT EXISTS `Users` ( `UID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `Fuid` varchar(100) NOT NULL, `Ffname` varchar(60) NOT NULL, `Femail` varchar(60) DEFAULT NULL, 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) or die( "Unable to connect"); $database = mysql_select_db(DB_DATABASE) or die( "Unable to select database"); ?>
» functions.php file contains a function to update the user information .
<?php require 'dbconfig.php'; function checkuser($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
logout not work. Any solutions?
It doesent logout from website. I try to logout and redirect to indexpage. After that I hit to login button and yahuu I am back logged in with the same Facebook account.
good
good
eish m having so many errors while trying to use this files.Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ‘(‘ in C:wampwww1353fbconfig.php on line 6
eish m having so many errors while trying to use this files.Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ‘(‘ in C:wampwww1353fbconfig.php on line 6
MUCHAS GRACIAS
MUCHAS GRACIAS
How can get email, existing code is not retuning email, please guide
Yes I have the same problem too! And I couldnt fetch other properties as well. Any idea on how to return email please? x
Terry’s comment below was the solution! However, I am unable to return other properties… x
How can get email, existing code is not retuning email, please guide
Yes I have the same problem too! And I couldnt fetch other properties as well. Any idea on how to return email please? x
Terry’s comment below was the solution! However, I am unable to return other properties… x
Can you tell me, if this code use API v1 or API v2 facebook login?
Can you tell me, if this code use API v1 or API v2 facebook login?
I get this error : Fatal error: Uncaught exception ‘Exception’ with message ‘The Facebook SDK v4 requires PHP version 5.4 or higher.’ in /home/u240484366/public_html/autoload.php:32 Stack trace: #0 /home/u240484366/public_html/fbconfig.php(4): require_once() #1 {main} thrown in /home/u240484366/public_html/autoload.php on line 32
Can anyone help me pls… Thanks in advance
Have your hosting provider upgrade php to 5.4
I get this error : Fatal error: Uncaught exception ‘Exception’ with message ‘The Facebook SDK v4 requires PHP version 5.4 or higher.’ in /home/u240484366/public_html/autoload.php:32 Stack trace: #0 /home/u240484366/public_html/fbconfig.php(4): require_once() #1 {main} thrown in /home/u240484366/public_html/autoload.php on line 32
Can anyone help me pls… Thanks in advance
Have your hosting provider upgrade php to 5.4
Hello, I was able to get other properties ,ie. locale with
$fLocale = $graphObject->getProperty(‘locale’);
however, I would like to ask how could I get the “name” of “location”? I have tried the following but does not work…
$fHomeTown = $graphObject->getProperty(‘hometown’)->getProperty(‘name’);
OR
$fHomeTown = $graphObject->getProperty(‘hometown’)->’name’;
$fHomeTwn = $fHomeTown->getProperty(‘name’);
It would be nice if someone could help me with it, cheers!
x
Hello, I was able to get other properties ,ie. locale with
$fLocale = $graphObject->getProperty(‘locale’);
however, I would like to ask how could I get the “name” of “location”? I have tried the following but does not work…
$fHomeTown = $graphObject->getProperty(‘hometown’)->getProperty(‘name’);
OR
$fHomeTown = $graphObject->getProperty(‘hometown’)->’name’;
$fHomeTwn = $fHomeTown->getProperty(‘name’);
It would be nice if someone could help me with it, cheers!
x
i use SDK PHP 4 after login i don’t get any thing just blank page this url http://www.ahzadigital.net/index.php?code=AQCnwlXL-DW9VinW5ewVzPtQ5qPbH1nGsZLZixouBTC7fN7977bcph6b0KwzXMgxh6sElHSWRBNrxDZ-gngL_IdSj8HxWNfi33sclCXb8Q9opgGqnao-wxE-YPtMsOv_TZUoQVgmyjESnnogZs8Ev5OW6JvdoXbHcw0ZYRJqx5tjihAoGjMJStheqNC73eODJd6liOzR2K-xDd-Ii0l4l5KgLOyq1inCplvKjp5oTMI_1VxHouaq6n0iwGgYO57afbP3hPbEW18vM_tt0RjSGBfNXkniPHJY0HJaLMnuodvcFU8mhxhODMXRGkpDcks1Em4&state=31a1599afa446e38ac175d10757fdc40#_=_ what’s happen ? thanks a lot
i use SDK PHP 4 after login i don’t get any thing just blank page this url http://www.ahzadigital.net/index.php?code=AQCnwlXL-DW9VinW5ewVzPtQ5qPbH1nGsZLZixouBTC7fN7977bcph6b0KwzXMgxh6sElHSWRBNrxDZ-gngL_IdSj8HxWNfi33sclCXb8Q9opgGqnao-wxE-YPtMsOv_TZUoQVgmyjESnnogZs8Ev5OW6JvdoXbHcw0ZYRJqx5tjihAoGjMJStheqNC73eODJd6liOzR2K-xDd-Ii0l4l5KgLOyq1inCplvKjp5oTMI_1VxHouaq6n0iwGgYO57afbP3hPbEW18vM_tt0RjSGBfNXkniPHJY0HJaLMnuodvcFU8mhxhODMXRGkpDcks1Em4&state=31a1599afa446e38ac175d10757fdc40#_=_ what’s happen ? thanks a lot
hello, I activated the facebook connect. But when I login I dont get the authorization screen. how do I make see?
Thank You
this page:
https://developers.facebook.com/docs/facebook-login/permissions/v2.2#reference-public_profile
you do not understand what code to write and where to put it
hello, I activated the facebook connect. But when I login I dont get the authorization screen. how do I make see?
Thank You
this page:
https://developers.facebook.com/docs/facebook-login/permissions/v2.2#reference-public_profile
you do not understand what code to write and where to put it
hello, I activated the facebook connect. But when I login I dont get the authorization screen. how do I make see?
Thank You
this page:
ithttps://developers.facebook.com/docs/facebook-login/permissions/v2.2
you do not understand what code to write and where to put
hello, I activated the facebook connect. But when I login I dont get the authorization screen. how do I make see?
Thank You
this page:
ithttps://developers.facebook.com/docs/facebook-login/permissions/v2.2
you do not understand what code to write and where to put
as you add this code in the page?
https://developers.facebook.com/docs/facebook-login/permissions/v2.2
as you add this code in the page?
https://developers.facebook.com/docs/facebook-login/permissions/v2.2
I did not see your solution for the problem
“This webpage has a redirect loop”
So, your code did not work.
awesome 100% workinh 🙂
hello bro u get the output 100%
pls help me i use above fb code its nice work but one error issue i can’t get EMAIL in redirect details ??
Hi Venky,
To get email id, you have to replace $loginUrl = $helper->getLoginUrl();
with $loginUrl = $helper->getLoginUrl( array(‘scope’ => ’email,read_stream’)); in fbconfig.php in line no. 49…
Aadrsh also give the solution for this(in above comments)
Regards,
SanDeep Malik
thanks bro
please any one can tell me how to reterive user interest from facebook within this code
only email which is taken at the time of creating app key and secret can login ,other user can’t login
hello, i was trying to do login for by facebook for my website but it show me the following error.”
This webpage has a redirect loop”
reply me argent plz.
Hello,
I am getting this error
Fatal error: Class ‘FacebookEntitiesAccessToken’ not found in /home/u552202982/public_html/src/Facebook/FacebookSession.php on line 74