Login with Facebook

Posted 3 years ago     3556      PHP Login Facebook

More and more sites, are using login with Facebook functionality as it offers an easy way for users to login to a site without registering a bunch of their own details. Having this functionality is good as you can request details form users without them having to spend the time filling them out themselves. Here's how to do implement the feature on your site in 6 steps.

Step 1
Log into Facebook, go to the developer apps page, and click 'Create New App':
Create new Facebook App

Step 2
Fill in the required fields:
Fill in Facebook App fields

You'll be asked to fill in a capture, do that and proceed to the next step.

Step 3
Once the capture is filled out, you will land on your app Dashboard where you can change your apps settings, upload imagery for your app, etc. For now we only need the App, and App secret ids. Keep this tab open and head over to Facebook's PHP SDK on GitHub. Download the 3 files from here and add them into your website/app structure. You'll be including facebook.php somewhere so make sure that wherever you put facebook.php you also add the other two files into the same directory.

Step 4
The code for the app is straight forward. It requires a login URL which points to Facebook, logs the user in and redirects them back to your site:

<?php session_start(); require_once 'facebook/facebook.php'; $app_config = array( 'appId' => 'app id', 'secret' => 'app secret key', 'allowSignedRequest' => false ); $facebook = new Facebook($app_config); $facebook_user = $facebook->getUser(); if($facebook_user) { $facebook_login = array( 'url' => $facebook->getLogoutUrl(), 'text' => 'Log out', 'data' => $facebook->api('/me?fields=first_name,last_name,name,gender,location,email','GET') ); } else { $facebook_login = array( 'url' => $facebook->getLoginUrl(array('scope' => 'user_location,email')), 'text' => 'Log in', 'data' => null ); } ?> <!DOCTYPE html> <html> <head> <title>Login with Facebook Snippet</title> </head> <body> <a href="<?php echo $facebook_login['url']; ?>"><?php echo $facebook_login['text']; ?></a> </body> </html>

In the above code you may notice that I am using the $facebook->api() to request permission. I'm requesting some information from the user which will use my app to login with Facebook. Try to limit the amount of information you collect about the user, as this might put off a user from accepting the app. You can configure permissions in the App Dashboard settings.

Step 5
As we are now the app won't work, we need to set up some specific URL for our app to redirect properly, once users are logged in. Go to your App's Dashboard and configure your domain and contact email. Note: you must have a contact email otherwise your app won't work.

Facebook App Dashboard domain and contact emil

Step 6
Head over to your App's Dashboard and go to 'Status & Review'. Here you can enable your app to go live:

Make your Facebook App live for other people to use

Now you're app is live and you can implement the easy 'Login with Facebook' feature and collect useful social data on your users. Once users click your login link, they will be presented with a popup like this:

Login with Facebook, accept


There are no comments yet.

You need to be logged in to post comments.

Welcome to Snippet Repo!

Discover, share and save useful code snippets.

Join our community over over 2,000 members! Currently a 48% acceptance rate. Apply for membership →