Facebook Login Feature For Your Website

Logging in users for various purposes is now becoming mandatory with the increasing usage of the Internet. Most users may not like to create an account on your website for small purposes. Moreover, filling up your customized signup form and remembering your site’s ID and passwords may seem tedious and uninteresting.

In these situations, users prefer to have a common login credential, which is possible by using third party APIs. Facebook is one of the most used social media along with other services like Gmail, Twitter, Yahoo, etc. Today, in this post we will discuss logging in users with Facebook for your sites.

What you need is to have a Facebook App of yours, which can be generated at developers.facebook.com. For a detailed guide on creating a Facebook app, please visit here. Once, you are done with creating the Facebook application, you can proceed with the following steps.

Facebook Login: Logging in users with Facebook API

Now, we will set up the basic Facebook API for your site. Use the below given code where you need to put your login link.

<?php

	error_reporting(E_ALL);
	ini_set('display_errors','On');
	
	// Pass session data over.
	if(!session_id()) {
	    session_start();
	}
	
	
	// Include the required dependencies.
	require_once( 'inc/facebook/autoload.php' );
	
	
	// Initialize the Facebook PHP SDK v5.
	$fb = new Facebook\Facebook([
			'app_id'                => 'xxxxxxxxxxxxxxx',
			'app_secret'            => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
			'default_graph_version' => 'v2.3',
	]);
	
	
	// Choose your app context helper
	$helper = $fb->getCanvasHelper();
	
	$helper = $fb->getRedirectLoginHelper();

In the above code snippet, put in your app_id and app_secret. Now, you need to ask users for various permissions for the data that your FB app can access.

$permissions = ['email', 'user_posts'];

Now, you need to add a callback URL, which has to be the page where you want the user to be redirected after they sign in with Facebook. Also, we will setup the login URL.

$callback = 'http://yoursite.com/index.php';
$loginUrl    = $helper->getLoginUrl($callback, $permissions);

Finally, now it is time to make a login URL. You can create the Facebook login URL that matches your sites’ view and feel. Here is the code to make a simple URL.

echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

Related Post

About the Author

Rashika Raina

Rashika is working as Business Development Executive at Regur technology Solution. She has a zeal to learn & grow and passion for work in any competitive situation. Her interest lies in Sales, Marketing Strategies, International Marketing, Business development. Apart from this, she has a good hold in communication, presentation and Market research skills.