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_request parameter 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), and liked (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

Back to top
Tagged with:
 
  • http://cv.zerkms.com zerkms

    I think that this article should also be supplemented with how to check if some page (not a page of a canvas app, but a page of 3rd party site with like-button installed) was whether liked by user or not.

    • http://www.masteringapi.com Ibrahim Faour

      Do you mean with the edge.create event?

  • Adrian Cotter

    Thanks for this. Super helpful. One thing to note (for those of us using different languages), the request is POST request. Tripped for a while.

    • http://www.masteringapi.com Ibrahim Faour

      Thanks for your comment. I’m using $_REQUEST because it’s recommended by Facebook. Please feel free to drop me an email if you want to share the same code in other languages.

      • Bdmkteam

        how can i get
        $_REQUEST["signed_request"]; data in javascript other than PHP…my requirement is toget the page id to which my page tab is added using java script not in PHP. as i am restictedto use php in one my servers.

  • Neven

    There si problem in FF I think :(

    • http://www.masteringapi.com Ibrahim Faour

      What kind of problem? I’m using it in all my projects. And it’s working just fine.

  • Andy

    How i can implement this data if i want to check it on my website, for example user comes to my website: example.com , and i check if he is already a fan/liked my specified FB page, if he is – i say hello, if not i ask him to like it. Any ideas? Because now i implemented FQL query to do this, and i need everytime to ask new user to allow access to check this simple data, that is really annoying and scaring people. Any help would be very helpful and useful for other visitors too! Maybe it’s possible to imlement this trick that you wrote here? Thanks!

    • http://www.masteringapi.com Ibrahim Faour

      I don’t think this is possible, but you can always use the Facebook Social Plugins for this.

  • George

    Using Javascript SDK:

    FB.api({ method: ‘fql.query’, query: ‘SELECT uid FROM page_fan WHERE uid = me() AND page_id = ‘ + fan_page_id },
    function(result)
    {
    if (result.length) //Our fans
    {
    //do something;
    }
    else //Not our fans
    {
    //do something else;
    }
    });

    • http://www.masteringapi.com Ibrahim Faour

      This requires the user to actually “allow” your application which is not intended here. Thanks for your addition anyway!

      • http://www.facebook.com/people/Elida-Fernandez/1610977500 Elida Fernandez

        Do you only need to allow the application or you need any extended permisions? I need to know if a user is a fan a of a certain page inside an application once the user has allowed the app. Something similar to this: http://www.facebook.com/descubreirlanda?sk=app_183615198329795 once you have allowed the application (only basic information) you need to like the page to proceed.

        • http://www.masteringapi.com Ibrahim Faour

          You need the user_likes permission

  • http://www.facebook.com/22markets Sebastian Lorenz

    Hi,

    I tried to test this snippet for my app, but it always returned “You are not a Fan”.
    I’ve put this snippet in an .php page and call it with my app.

    It doesn’t matter if Ilike or not a page. Please could you help me.

    • http://www.masteringapi.com Ibrahim Faour

      Make sure you are receiving the signed_request. Put this in the first line just to check : var_dump($_REQUEST["signed_request"]);

      If it’s not set, contact Facebook about the issue.

      • http://twitter.com/smartssa Darryl E. Clarke

        If you’re not getting a ‘signed_request’, your application was created before it was enabled by default.  You can go to your application settings, ‘Advanced’ and under the migrations section you will find the place to enable the ‘signed_request’ …

  • Glenn Fundimera

    hey there…. it’s not working on my page…. can you help me out???

    • http://www.masteringapi.com Ibrahim Faour

      Make sure you are actually getting the signed_request, also double check your Facebook App settings. also make sure you are not sending any GET parameters as the signed_request may get lost.

      After all this, try to contact Facebook about this.

  • http://www.facebook.com/madslarsen Mads Gorm Larsen

    Works for me – brilliant I might ad :) Thanks a bunch for sharing this tip. 

  • http://www.facebook.com/yapser Jasper Gremmen

    Is there a way to achieve this with Javascript only?

  • Srikanth

    hey ,
    how to get in fan page guest name..
    right now we showing like  “You are not a fan!”…so i need ” welcome srikanth, You are not a fan”…how can get guest username(full name)..

    • http://www.masteringapi.com Ibrahim Faour

      As mentioned in the “Notes” section, you can’t have a detailed information about the user “unless” he authorize your application!

      Which in my opinion (based on what you are asking for) is not worth it!

      • http://www.facebook.com/tjramage Tim Ramage

        Yes, this is actually possible, and I would also like to know how! Have a look here: http://www.facebook.com/nextonline

        They manage to grab the name of the user even if you haven’t ‘liked’ their page…

        • Ibrahim Faour

          They are using FBML, it’s no longer available for new Apps and will be stopped soon!

        • http://www.masteringapi.com Ibrahim Faour

          They are using FBML, it’s no longer available for new Apps and will be stopped soon!

          • http://www.facebook.com/tjramage Tim Ramage

            Ahhh I see. Ok, thanks for letting me know! You’re code is very helpful :-)

  • Anonymous

    Hi,

    I added the PHP code to my facebook page, but it does nothing. I tried adding the line:
        var_dump($_REQUEST["signed_request"]);but this did nothing. Should I be contacting Facebook?

    Thanks

    • http://www.masteringapi.com Ibrahim Faour

      Refer to this thread, also note that if you are doing any redirection the POST parameters will be lost.

  • Farid

    is it possible to have a btn like in an application and this btn is to like another fan page?

    i mean to acces to this application we have to be fan of another  page

    any help?

  • Henriquevidalfoletto

    DUDE, YOU ARE AWESOME, EPIC JOB, THANKS A LOT, !!!!!!!!!!!!!! I WORKED JUST HOW I WANTED TO, DUDE, THAKS A LOT AGAIN!

  • http://www.facebook.com/Jigster Jirka Jigster Řízek

    Hi, first of all, thanks for the code, it helped me. I have a subsequent problem, though.
    Instead of “echo”, I used “include” with php file for liked content and for unliked. Right now the content is just one image, which fits into the dimensions of the canvas. But browsers are still displaying horizontal scrollbar. I tried aligning the image, creating a wrapper, using the image as fixed background, but nothing works.
    Is there perhaps any way to not use “include” and display the particular image directly? Or a piece of code – you know, so I don’t have to create other files?
    Or is there a way to get rid of the scrollbar? Either way will be fine.
    Thank you Very much

  • http://www.facebook.com/m1tk00 Mitko Mitkoo

    I have problem with this script … getting the following error ”
    signed_request was not found!” any help please?

  • yadi666

    yeahhh.. super helpfull.. thanks bro

  • http://www.facebook.com/profile.php?id=100002322474214 Chun AhMoi

    If I have a Like button in my webpage, how to detect a user whether is my fan once he/she lands on my webpage? Please help.