I am using facebook registration on my web project.
The registration works fine in most of the cases, but sometimes the facebook iframe does not send the fbuid to the php script…
Is this a facebook bug? Any workaround?
Here is the facebook ifranme:
<iframe src="https://www.facebook.com/plugins/registration?
client_id=####&
redirect_uri=http://****/pre-login/facebook/fb_register_respond.php&
fields=name,email"
scrolling="auto"
frameborder="no"
style="border:none;display:block;"
allowTransparency="true"
width="350"
height="330">
</iframe>
In some registrations the fbuid is false.
If the user is not connected to facebook or is already registred with facebook, then the iframe will not be displayed.
Here is the php code:
define('FACEBOOK_APP_ID', '####');
define('FACEBOOK_SECRET', '####');
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);
// Die email Adresse des Users.
$email = $response['registration']['email'];
// Das Passwort (fb-user-id) des Users.
$fbuid = $response['user_id'];
} else {
// Im Fehlerfall.
echo '$_REQUEST is empty';
exit;
}
By default the Registration plugin allows users to not use their existing Facebook account to sign up for your service – they can delete the pre-filled data, and type in their own, or without even being logged in to Facebook (in which case they are offered a blank form at first, with the additional possibility to log into Facebook if they have an account).
Logically, if the user chooses not to use their Facebook account, but just fill out the form with their own data – you get no Facebook user id transmitted to your server on submit, because there is none.
If you don’t want this, and want users to only use their existing Facebook account to sign up (and thereby be sure to always get a FB user id) – then set the parameter
fb_onlyto true while calling the Registration plugin.