How to create Login with facebook in php

Nowadays filling big registration forms are time consuming and boring , user like fast, simple and secure one click and website will get valid user data from Facebook, Google, Microsoft and Linkedin.Lets start creating login with facebook first!First Step : Create Facebook APP ID and APP secret on facebook developer page.go to Facebook Developer page, click on skip and create app id (as shown below)A popup window will open
Fill the form by entering
  • Display Name
  • Namespace
  • Kindly choose category based on the type.
and click create App ID Button

On the left hand side you will find tabs click on settings tab 
Under settings tab, Provide values for App domain ( Eg:www.lanbytes.com ), Contact Email and click Add Platform.On popup choose website and Provide Values for Site URL and Mobile site URl ( Optional )Now under App Review, Click the button to make you App live .Step 4 : Download the package from this link Package File Link

Step 5 : Now open fbconfig.php file and enter your app ID, secret and change domain name .
 // init app with app id and App secret   
 FacebookSession::setDefaultApplication('64296382121312313','8563798aasdasdasdweqwe84' );  
 // login helper with redirect_uri  
 $helper = new FacebookRedirectLoginHelper('http://www.krizna.com/fbconfig.php' );  

 session_start();  
 // added in v4.0.0  
 require_once 'autoload.php';  
 //require 'functions.php';  
  use Facebook  
 FacebookSession;  
 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 secretFacebookSession::setDefaultApplication( '64296382121312313','8563798aasdasdasdweqwe84' );// login helper with redirect_uri  $helper = new FacebookRedirectLoginHelper('http://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 sessionif ( 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);}?> index.php  
Step 7 » You can change this file as per your need . Split this file into 2 parts before login and after login.session_start();
?>

 
          Hello  Welcome to "facebook login" tutorial 











  • Logout          Login with Facebook           Not Connected      Login with Facebook         
    logout.php file overviewLogout.php file is used only to destroy facebook session and return back to your home page .Step 8 » Enter your home page in the code to redirect after logout.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: " ."http://www.krizna.com/home.php"); ?> 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.define('DB_SERVER', 'localhost');define('DB_USERNAME', 'username');    // DB usernamedefine('DB_PASSWORD', 'password');    // DB passworddefine('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 . 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); } }?>ncomment the below lines in fbconfig.phprequire 'functions.php'; // Include functionscheckuser($fbid,$fbfullname,$femail); // To update local DBThat’s it .. now you can store the values locally .













    Post A Comment
    • Blogger Comment using Blogger
    • Facebook Comment using Facebook
    • Disqus Comment using Disqus

    No comments :