Implementing User Signup using Facebook Data.
💻 coding

Implementing User Signup using Facebook Data.

2 min read 428 words
2 min read
ShareWhatsAppPost on X
  • 1Users can sign up on your website using Facebook data through a simple registration plug-in.
  • 2Creating a Facebook App is essential for implementing the registration process, requiring an App ID and App Secret.
  • 3The registration form can be designed using an iframe, allowing customization of fields beyond Facebook's default options.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Users can sign up on your website using Facebook data through a simple registration plug-in."

Implementing User Signup using Facebook Data.

Using Facebook registration plug-in users can easily sign up on your website using Facebook data. With one simple Facebook login, the form will be filled with user appropriate data. This plug-in is a simple iframe you can place it anywhere on your webpage. You can also add a custom field if Facebook doesn’t have.

1. Creating a Facebook App

a) The first step is to create a Facebook App.

b) Give a name for the Application and agree with the Terms and Conditions.

c) On the Web Site tab give the values for siteURL and site domain

d) Store the App ID and App Secret.

2. Designing the Database

Create a table depending upon the fields we required in the registration process.

CREATE TABLE users
(
uid int(11) PRIMARY KEY AUTO_INCREMENT,
fullname varchar(50),
email varchar(50) UNIQUE,
password varchar(50),
gender varchar(6),
dob varchar(16)
);

3. Designing the Registration Form

a) We are using iframe provided by facebook to display the registration form.

b) You all have to do is just replace client_id and redirect_uri with your Facebook App Id and the redirection url.

c) In this tutorial, we are designing a form with name, email, password, gender, date of birth and captcha.

registration.php

<iframe allowtransparency="true" frameborder="no" height="600" scrolling="auto" src="http://www.facebook.com/plugins/registration.php?
client_id=183620578322567&
redirect_uri=http://example.com/store_user_data.php?&
fields=[
{"name":"name"},
{"name":"email"},
{"name":"password"},
{"name":"gender"},
{"name":"birthday"},
{"name":"captcha"}
]"
scrolling="auto"
frameborder="no" 
style="border: none;" 
width="500"
height="600">
</iframe>

4. Reading and Storing the Callback Data

store_user_data.php

<?php
define('FACEBOOK_APP_ID', 'your_app_id'); // Place your App Id here
define('FACEBOOK_SECRET', 'your_app_secret'); // Place your App Secret Here

// No need to change the function body
function parse_signed_request($signed_request, $secret) 
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}

// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) 
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) 
{
return base64_decode(strtr($input, '-_', '+/'));
}
if ($_REQUEST) 
{
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);

$name = $response["registration"]["name"];
$email = $response["registration"]["email"];
$password = $response["registration"]["password"];
$gender = $response["registration"]["gender"];
$dob = $response["registration"]["birthday"];

// Connecting to database
mysql_connect('DATABASE_HOST', 'YOUR_USERNAME', 'YOUR_PASSWORD');
mysql_select_db('YOUR_DATABASE');

// Inserting into users table
$result = mysql_query("INSERT INTO users (name, email, password, gender, dob) VALUES ('$name', '$email', '$password', '$gender', '$dob')");

if($result){
// User successfully stored
}
else
{
// Error in storing
}
}
else 
{
echo '$_REQUEST is empty';
}
?>

Adding a custom field

If you want to request user for additional information that facebook may not have then added a custom field in JSON string.

{"name":"phone", "description":"Phone Number", "type":"text"}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 21 August 2019 · 2 min read · 428 words

Part of AskGif Blog · coding

You might also like

Implementing User Signup using Facebook Data. | AskGif Blog