Like Us on Facebook

How to login or signup with facebook API using PHP

Hi friends!! Today we are going to learn an interesting topic, that is how to login or signup with facebook API using PHP.  For every user communication website they must need a user login panel and for login they must need registration for proper authentication. Here comes beauty of social media like facebook, google plus, twitter for their signup process. And also users don’t have more time to fill all the information so they prefer social media login. So this tutorial will teach you how to signup or login with facebook using php.  



At first you have to login https://developers.facebook.com/ . Now you need to register facebook developer account. So click on register now button.



Here you have to accept facebook policy and click register. So you have successfully registered as a facebook developer. Now you can add facebook into your app or website.

Afetr login into your facebook developer account.Click Add a new app



Next you have to select area where you want to use this app, here I am using website.



Next click to the skip and create app id



Now you have to write display name and select category. Namespace is not necessary.  But remember that display name must be unique. Fill out this captcha.
 


Now your app has been created.




In this area you can see your app display name and you  get your appid and appsecret code. Actually  This dashboard is looking very complicated but you have to do very simple few steps here.Then give your contact information and click on add platform, select your area once again. Give your site URL and write your domain name inside app domains text box.



Now click on status and review on your left side bar, and turn on this app for make this visible to everyone.Your app is now live and you have implement on you website.


 

The script contains three folders called inc,includes and images with PHP files.

base_facebook.php // Class Get user Info

facebook.php // Class Get user Info

config.php // Configuration file

functions.php // Database Connectivity

images

index.php
// Main index file show data

logout.php // Destroy Your Session
 

 

Config.php

<?php
          include_once("inc/facebook.php");
         
$appId = ''; 
         
$appSecret = '';
         
$homeurl = 'http://localhost/facebook_login/'; 
         
$fbPermissions = 'email';

         
//Call Facebook API
          $facebook = new Facebook(array(
           'appId'  =>
$appId,
          'secret' =>
$appSecret

           ));
         
$fbuser = $facebook->getUser();
?>



functions.php

Create Table users
 (
   
 oauth_uid,
     oauth_provider,
     fname,
     lname,
     email,
     gender,
     locale,
     picture

 );

// Database Connectivity, Insert and Update Code Here.

 


index.php

<?php

    include_once("config.php");
    include_once("
includes/functions.php");
    
//destroy facebook session if user clicks reset
    if(!$fbuser){
        
$fbuser = null;
        
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
        
$output = '<a href="'.$loginUrl.'"><img src="images/fb_login.png"></a>';     
    }
    else{
        
$user_profile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
        
$user = new Users();
        
$user_data = $user->checkUser('facebook',$user_profile['id'],$user_profile['first_name'],$user_profile['last_name'],$user_profile['email'],$user_profile['gender'],$user_profile['locale'],$user_profile['picture']['data']['url']);
        
        
    }
?>

    <html>
        
<head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Login with Facebook using PHP by CodexWorld</title>
            <style type="text/css">
            h1{font-family:Arial, Helvetica, sans-serif;color:#999999;}
            </style>
        </head>

    
        <body>
            <div>
                
<?php echo $output; ?>
            </div>
        
        </body>
    
    </html>


logout.php

<?php

    include_once("config.php");
    if(
array_key_exists('logout',$_GET))
    {
      
 $facebook->destroySession();
        
session_start();
        
unset($_SESSION['userdata']);
        
session_destroy();
        header("
Location:index.php");
    }


?>

 

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.