Login with Facebook Account using PHP
In this article, We will explain how to create a login with Facebook account using PHP. Here we will create a system where the user can log in with his Facebook account on your website. So, in this post, we share a tutorial on how to login or sign in or sign up with a Facebook account in your PHP web application.
To login with Facebook account using PHP, Facebook PHP SDK has provided Facebook OAuth API, which is very easy and useful to implement login using a Facebook account on your website. The Facebook login product provides rights to the user to login to the website using their Facebook account credential without registering on that particular website.
In this example, we will use the Facebook PHP SDK library, this library gives users permission to sign in with their existing Facebook accounts on your website. Below, you can find how to integrate Facebook login API into your PHP website and you can also find the process of how to get Facebook API id and how to integrate the Facebook SDK library into your existing PHP library to login.
Create Facebook App
Now we need to app id facebook and App Secret for Facebook login. first we have to create the Facebook app. so you can see below steps.
- First login with your Facebook account and Goto Facebook for Developers page https://developers.facebook.com/apps/
- click on My apps menu at the top navigation bar and click on Create App link
- select app types
- Enter the App Display Name and App Contact Email.
- Click on the Create App button.
- In the right sidebar menu, navigate to Settings > Basic menu
- Add App Domains and select the Category of your App.
- Click the Save Changes.
- In the right sidebar menu, navigate to the Add a Product
- Select Facebook Login product.
- select the web platform.
- Enter the Site URL and Save.
- In the right sidebar menu, navigate to the Facebook Login > Settings menu.
- Add Valid OAuth Redirect URIs field
- Click the Save Changes.
- In the right sidebar menu, navigate to the Settings > Basic menu.
- get App ID and App Secret
Install / Download Facebook SDK for PHP
After we got the Facebook app id and Secret key, now we need to download the Facebook Sdk for PHP library. For this, we need to go to the command prompt and first type the command composer, after that, we need to run the following command.
1 | composer require facebook/graph-sdk |
Facebook API configuration and set variables
config.php
now, first, we need to create a config.php file. after that, we will configure the Facebook app id and secret key and add the Facebook SDK library in this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php require_once 'vendor/autoload.php'; if (!session_id()) { session_start(); } $facebook = new \Facebook\Facebook([ 'app_id' => 'enter facebook app id', 'app_secret' => 'enter facebook app secret key', 'default_graph_version' => 'v2.10' ]); ?> |
index.php
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <?php include('config.php'); $facebook_output = ''; $facebook_helper = $facebook->getRedirectLoginHelper(); if(isset($_GET['code'])) { if(isset($_SESSION['access_token'])) { $access_token = $_SESSION['access_token']; } else { $access_token = $facebook_helper->getAccessToken(); $_SESSION['access_token'] = $access_token; $facebook->setDefaultAccessToken($_SESSION['access_token']); } $_SESSION['user_id'] = ''; $_SESSION['user_name'] = ''; $_SESSION['user_email_address'] = ''; $_SESSION['user_image'] = ''; $graph_response = $facebook->get("/me?fields=name,email", $access_token); $facebook_user_info = $graph_response->getGraphUser(); if(!empty($facebook_user_info['id'])) { $_SESSION['user_image'] = 'http://graph.facebook.com/'.$facebook_user_info['id'].'/picture'; } if(!empty($facebook_user_info['name'])) { $_SESSION['user_name'] = $facebook_user_info['name']; } if(!empty($facebook_user_info['email'])) { $_SESSION['user_email_address'] = $facebook_user_info['email']; } } else { $facebook_permissions = ['email']; $facebook_login_url = $facebook_helper->getLoginUrl('https://xpertphp.com/demos/php/php_facebook_login/', $facebook_permissions); $facebook_login_url = '<div align="center"><a href="'.$facebook_login_url.'"><img src="facebook-login-button.png" /></a></div>'; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login with Facebook Account using PHP</title> <meta content='width=device-width, initial-scale=1, maximum-scale=1' name='viewport'/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="container"> <br /> <h2 align="center">Login with Facebook Account using PHP</h2> <br /> <div class="panel panel-default"> <?php if(isset($facebook_login_url)) { echo $facebook_login_url; } else { echo '<div class="panel-heading">Welcome User</div><div class="panel-body">'; echo '<img src="'.$_SESSION["user_image"].'" class="img-responsive img-circle img-thumbnail" />'; echo '<h3><b>Name :</b> '.$_SESSION['user_name'].'</h3>'; echo '<h3><b>Email :</b> '.$_SESSION['user_email_address'].'</h3>'; echo '<h3><a href="logout.php">Logout</h3></div>'; } ?> </div> </div> </body> </html> |
delete.php
1 2 3 4 5 | <?php // destroy all session session_destroy(); header('location:index.php'); ?> |
Read Also
Login With Google Account Using PHP
PHP Login And Registration Example
Login And Registration Example Using Pdo