Login with Facebook SDK and native php

Login social


Getting all your Facebook data is very easy with Facebook Login and native php

Facebook Login It is a very interesting tool that allows to speed up the login in websites and applications. Thus, when we enter with our Facebook user, the application or website takes the data we have associated with the profile, such as: name, surname, age range, sex and email. The above turns out to be more agile and comfortable for the user as beneficial for the system administrator.

Next, I present to you how the relatively basic project to learn how to develop software with the php programming language is developed.

Project structure


<?php session_start(); include_once("facebook-sdk-5/src/Facebook/autoload.php"); $fb = new Facebook\Facebook(array( "app_id" => "*******", // Replace with your app id "app_secret" => "*******************", // Replace with your app secret "default_graph_version" => "v3.2", )); $helper = $fb->getRedirectLoginHelper(); ?>

Version 5 of the Facebook SDK is used, which is in charge of capturing the app id variables and the secret key that Facebook provides. So what we are going to do here is assign the keys to the Facebook library, so that it can connect us well.


<?php include_once("config.php"); try { $accessToken = $helper->getAccessToken(); } catch(Facebook\Exceptions\FacebookResponseException $e) { echo "Graph returned an error: " . $e->getMessage(); exit; } catch(Facebook\Exceptions\FacebookSDKException $e) { echo "Facebook SDK returned an error: " . $e->getMessage(); exit; } if (!isset($accessToken)) { if ($helper->getError()) { header("HTTP/1.0 401 Unauthorized"); echo "Error: " . $helper->getError() . "\n"; echo "Error Code: " . $helper->getErrorCode() . "\n"; echo "Error Reason: " . $helper->getErrorReason() . "\n"; echo "Error Description: " . $helper->getErrorDescription() . "\n"; } else { header("HTTP/1.0 400 Bad Request"); echo "Bad request"; } exit; } if(!$accessToken->isLongLived()){ try { $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo "<p>Error getting long-lived access token: " . $e->getMessage() . "</p>\n\n"; exit; } } $res = $fb->get("/me",$accessToken->getValue()); $fbUser = $res->getDecodedBody(); $resImg = $fb->get("/me/picture?type=large&amp;redirect=false",$accessToken->getValue()); $picture = $resImg->getGraphObject(); $_SESSION["fbUserId"] = $fbUser["id"]; $_SESSION["fbUserName"] = $fbUser["name"]; $_SESSION["fbAccessToken"] = $accessToken->getValue(); header("Location: welcome.php"); exit; ?>
This file allows us to receive the response that Facebook returns in order to obtain the requested data, such as the profile photo and the username. This is how you can make the call of each of the variables that you want to obtain or bring to the application. Once you have brought the data you decided to obtain, you can save it in your database if you wish.


<?php include_once("config.php"); session_destroy(); unset($_SESSION["fbUserId"]); unset($_SESSION["fbUserName"]); unset($_SESSION["fbAccessToken"]); header("location: http://localhost/php-login-facebook/index.php"); /*change url for you localhost*/ exit; ?>

Logout is where the user's safe exit from the application is controlled by deleting their id and username.