Facebook Open Authentication in PHP

Open authentication is for providing access to the server resources after authorizing users via third party services. It will take place between open authentication providers and websites implementing open authentication.

In this tutorial, we are going to see about implementing Facebook open authentication with our website using PHP. We can achieve this login mechanism using Facebook API.

facebook_open_authentication

Steps for Facebook Open Authentication

  1. Register as Facebook developer.
  2. Create new Facebook application to get Facebook API key and secret key.
  3. Download and install Facebook SDK for PHP.

1. Register as Facebook Developer

First we should visit Facebook developer website to register as developer.

register_developer

After few steps, the Facebook developer registration will be confirmed.

2. Getting API Key and Secret Key

API Key and Secret Key will be created after we create a new Facebook application. With the reference of these keys, we can access Facebook API classes.

While creating new Facebook application, we need to provide application’s name, namespace, application’s domains, the purpose to integrate Facebook API and etc.

In this tutorial, we have an example for implementing Facebook login into our website. So, we need to set Site URL to be redirected after Facebook login.

edit_facebook_devapps

3. Download/Install Facebook SDK for PHP

We can download Facebook SDK files from https://developers.facebook.com/docs/php/gettingstarted/ for PHP. And then, we should unzip the downloaded SDK files and copy src/ folder to our application’s root directory.

Installing Facebook SDK

To install Facebook SDK into our website,

  1. Setting Facebook API configuration with our own API key and secret key.
  2. Include facebook.php and instantiate Facebook class.
  3. Use this API class instance to access its functions.

PHP example: Facebook Open Authentication

Let us create a PHP file named config.php to set API key and secret key.

config.php

<?php
$config = array();
$config['appId'] = 'API KEY';
$config['secret'] = 'SECRET KEY';
$siteConfig = array();
$siteConfig['sitePath'] = 'http://localhost:85/phppot_samples/facebook_open_authentication/';
?>

And then, include this configuration and Facebook API class files into a PHP file saved as menu.php. It contains login and logout links to handle authentication with our Facebook account.

menu.php

<html>
<head>
<title>Facebook Open Authentication</title>
<style>
body {
font-family: Arial;
}
.menubar {
background-color: #60CC93;
border: #000000 1px solid;
height:22px;
padding: 10px;
width:500px;
}
.menubar a{
font-size: 15px;
}
label {
font-size: 30px;
color: #FFFFFF;
margin-right:300px;
}
.btnlogin {
float:right;
}
</style>
</head>
<body>
<?php
session_start();
require_once("facebook.php");
require_once("config.php");
$facebook = new Facebook($config);

$loginURL = $facebook->getLoginUrl(array('redirect_uri' => $siteConfig['sitePath'] . 'menu.php?action=login'));
$logoutURL = $facebook->getLogoutUrl(array('next' => $siteConfig['sitePath'] . 'menu.php?action=logout'));

if(isset($_GET["action"]) && $_GET["action"] == "logout") {
unset($_SESSION["fb_".$config["appId"]."_access_token"]);
}
?>
<div class="menubar">
<?php	
if(isset($_SESSION["fb_".$config["appId"]."_access_token"])) {
?>
<label>Main Menu </label><a href="<?php echo $logoutURL; ?>" title="Facebook Login">Logout</a>
<?php
} else {
?>
<div class="btnlogin">
<a href="<?php echo $loginURL; ?>" title="Facebook Login"><img src="loginwithfacebook.png" alt="Facebook Login"></a>
</div>
<?php
}
?>
</div>
</body>

We are getting login and logout URL by using Facebook API class functions. These URLs are used for respective hyperlinks.

By using PHP Facebook SDK, the user sessions and cookies will remain even after logout. So, it is hard to display login or logout links based on the Facebook login status.

To solve this problem, we are sending last action performed with redirect URLs of getLoginUrl() and getLogoutUrl() functions. Based on these actions, we can maintain session to show respective hyperlinks.

output:

While running this program, it will show Facebook login button.

facebook_login_menu

While clicking this Facebook login button, it will redirect to the facebook login page.

facebook_login

After login, the menu bar will be,

facebook_logout_menu

Download Facebook Open Authentication in PHP Source Code

This PHP tutorial was added on October 28, 2013.

«

»