How to Create Login with Facebook in PHP?

How to Create Login with Facebook in PHP?

As we know, Facebook is a widely used social network and sometimes, we have to create a login with Facebook mechanism in our websites. This becomes even more essential if our websites provide feature/system such as reservation, booking, resource downloading/accessing or even eCommerce where users tracking becomes a requisite.

User often finds it lazy and irritating to register a new user account in your website to interact with your system. So, why not provide a login with Facebook mechanism which would allow users to interact with your system using their existing facebook account.

Below are the easy steps to create login with Facebook in your website:

1. Create a Facebook app:

You can create a facebook app via https://developers.facebook.com/apps/.

While setting up your app, make sure:

– When asked to select scenarios, choose Facebook Login.

– On the Facebook Login > Settings page, under Valid Oauth Redirect URIs, enter https://YOUR_DOMAIN/login-page/.

– On the Facebook Login > Settings page, you can also set a Deauthorize Callback URL that will be called when a user deauthorizes your app.

2. Integrate SDK:

You can get Facebook php sdk hosted at facebook-php-sdk. Download or clone the files and folders and paste the full folder inside theme or plugin. Then, rename it as fb-sdk.

Now, locate autoload.php inside the downloaded sdk folder and include\require it like this:

require BASE_PATH.'/includes/fb-sdk/autoload.php'; //make sure to use correct path

3. Creating login link:

Use namespace Facebook and class Facebook already defined inside the fb-sdk folder as shown:

<?php
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxx', // Replace {app-id} with your app id
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxx', // Replace {app-secret} with your app secret
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('https://YOUR_DOMAIN/login-page/', $permissions);
?>

<script type="text/javascript">
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
</script>
<a href="<?php echo htmlspecialchars($loginUrl);?>">Log in with Facebook!</a>

Once clicked, user will be redirect to the Facebook login page and upon login will ask for the permission. After granting the permission, user will be redirected to your website (redirect url set in the app).

#Get Logged In User Info

When the user is redirected after successful login, you might need to have information of the logged in user. So, below is the code on how to get logged in user info:

<?php
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxx', // Replace {app-id} with your app id
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxx', // Replace {app-secret} with your app secret
'default_graph_version' => 'v2.5',
]);

$helper = $fb->getRedirectLoginHelper();
$access_token = $helper->getAccessToken();
$user_details = "https://graph.facebook.com/me?access_token=" .$access_token;
$response = file_get_contents($user_details);
//print_r($response);
?>

In this way, a login with Facebook link can be created using sdk. We can actually create such social logins for google, vk and many other social profiles using their app and sdk.

Leave comment

Your email address will not be published. Required fields are marked with *.