Determine If The User Likes (Fan Of) Your Facebook Page When Landing On Your Page Tab
It seems that a lot of developers and admins Facebook pages don’t know that they can detect whether the visiting user is already a fan of the page or not.
When the user visit your Page Tab, Facebook will send you the usual signed_request but with additional parameter called page (reference):
When a user navigates to the Facebook Page, they will see your Page Tab added in the next available tab position. Broadly, a Page Tab is loaded in exactly the same way as a Canvas Page. When a user selects your Page Tab, you will received the
signed_requestparameter with one additional parameter,page. This parameter contains a JSON object with an id (the page id of the current page),admin(if the user is a admin of the page), andliked(if the user has liked the page). As with a Canvas Page, you will not receive all the user information accessible to your app in the signed_request until the user authorizes your app.
This parameter is crucial for a successful product branding and user conversion. We can’t stress enough how important this feature is. How to make use of this feature is really up to you and your imagination and there are a lot of real world examples on Facebook like the YouTube Page and Linkin Park Page.
Make use of the liked parameter
Reading the signed_request and extracting the data we need is really simple:
<?php
if(empty($_REQUEST["signed_request"])) {
// no signed request where found which means
// 1- this page was not accessed through a Facebook page tab
// 2- a redirection was made, so the request is lost
echo "signed_request was not found!";
} else {
$app_secret = "APP_SECRET";
$data = parse_signed_request($_REQUEST["signed_request"], $app_secret);
if (empty($data["page"]["liked"])) {
echo "You are not a fan!";
} else {
echo "Welcome back fan!";
}
}
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, '-_', '+/'));
}
?>So with the code above you can detect if the user likes your page and if not encourage him to do so!
Notes
- You can’t get the user id from the
signed_requestof the landing user. - Page Tabs can be tested locally, read our tutorial about Developing Facebook Application Locally for more information.
- http://cv.zerkms.com zerkms
- http://www.masteringapi.com Ibrahim Faour
- Adrian Cotter
- http://www.masteringapi.com Ibrahim Faour
- Bdmkteam
- Neven
- http://www.masteringapi.com Ibrahim Faour
- Andy
- http://www.masteringapi.com Ibrahim Faour
- George
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/people/Elida-Fernandez/1610977500 Elida Fernandez
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/22markets Sebastian Lorenz
- http://www.masteringapi.com Ibrahim Faour
- http://twitter.com/smartssa Darryl E. Clarke
- Glenn Fundimera
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/madslarsen Mads Gorm Larsen
- http://www.facebook.com/yapser Jasper Gremmen
- Srikanth
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/tjramage Tim Ramage
- Ibrahim Faour
- http://www.masteringapi.com Ibrahim Faour
- http://www.facebook.com/tjramage Tim Ramage
- Anonymous
- http://www.masteringapi.com Ibrahim Faour
- Farid
- Henriquevidalfoletto
- http://www.facebook.com/Jigster Jirka Jigster Řízek
- http://www.facebook.com/m1tk00 Mitko Mitkoo
- yadi666
- http://www.facebook.com/profile.php?id=100002322474214 Chun AhMoi
Advertisment
Recent Tutorials
- How To: Create A User Photo Albums Browser Using Facebook Graph API
- How To: Upload A Photo To A User’s Profile Using Facebook Graph API
- How To: Check Status And RSVP To Facebook Events Using Graph API & FQL
- Facebook Javascript SDK Best Practices
- How To: Create Facebook Events Using Graph API – Advanced






