Dejan Levec

How to display certain page content only to users who have liked our page on Facebook?

Sometimes it would be convenient if we could display portion of our web page only to people who have liked our page on Facebook.

For example, we decide to giveaway five concert tickets to random participants. We can get more recognizable if participants have to liked our page and possible publish that on their profile page. So let’s create example web page that where people can participate after following this procedure:

1. person connects our web site with Facebook

2. person likes our Facebook page

3. person fill out form with his information to participate in giveaway

 

Firstly we need to create Facebook page for our web site and get URL like this:

https://www.facebook.com/pages/Lemnin/145152362222716

Secondly, we need Facebook application which can be created in a few minutes on Facebook. We will get application id and secret key to use Facebook API.

Our giveaway page will look like this (step 1):

After clicking on Login with Facebook button, we get a nice popup saying what information will this application be able to access:

After user successfully connects our web site (application) with his Facebook profile, we show step 2:

In this step, user has to like our Facebook page, and after that we show him step 3 – final giveaway form:

We automatically fill out name field with information we get through Facebook API. Mind you, step 3 loads via AJAX, because requests to Facebook servers can sometimes be slow and we want to load our page as quickly as possible.

 

How will we do it?

We will use Facebook’s javascript API which allows us to connect our website with user’s Facebook account and allow them to like our page. As far as i know, we don’t even need server-side scripting to check Facebook Like status, however, user’s can bypass JS-only solution and therefore we will be using client-side javascript in connection with server-side PHP.

 

1. How to connect to Facebook API with PHP?

Connecting to Facebook API is pretty simple and everything you need is to create new instance of Facebook class and pass it array with your appId and secret key.

<?php
include (“facebook.php”);
$facebook = new Facebook(array(  ‘appId’  => ‘172133412841299’,
‘secret’ => ‘<secret key>’,
‘cookie’ => true,));

 

2. Is user logged in?

Facebook JS API creates session cookie when user logged in to Facebook comes to our page, so that we can get user’s information with PHP.

<?php
$session = $facebook->getSession();
$fbme = null;
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api(‘/me’);
} catch (FacebookApiException $e) {                }
}
?>

This code gets FB’s session, checks if user allowed us access to his information and fills variable $fbme with his data (username, first_name, last_name, etc.)

 

3. Has user liked our Facebook page?

<?php
$created_time = $facebook->api(
array( ‘method’ => ‘fql.query’,
‘query’ => ‘SELECT created_time FROM page_fan WHERE uid= ‘.intval($uid).’ AND page_id = 145152362222716′ )
);
?>

This uses FQL to check if user liked our page and returns date and time of when that happend. If user hasn’t liked our page, $created_time is empty.

 

With a bit of javascript magic and PHP we can create web page, that checks if user liked our page and if he is logged in.

Here is the download link: download

5 Responses to “How to display certain page content only to users who have liked our page on Facebook?”

  • Xander says:

    With the version 3.0 of the Facebook API the getSession() method no longer exists; you may want to look at updating your guide. 😉

  • bilow says:

    It keeps trying to connect to Lemn.in ??
    even tough i see the number of current likes (which is right), it keeps connecting to Lemn.IN?
    What am i doing wromg??
    I’ve only edited the page url in facebook_test.php and Page_ID in get_form.php

  • Do you have a test site somewhere to see if the code still works? I get an error occured in lemn.in ?

    The webpage is http://maartenprovo.be/projects/gate/facebook_test.php .

  • It seems the error is just to log-in-through facebook. The actual problem is, that It only shows the content when I’ve just liked it, not when I visit the page and already like it.

  • marcel says:

    Thx for the great post.I am planning to give away freebies to people and I need this “reveal website contents after our facebook page liked” script very much. However, I find that it is not working now.
    Would you mind updating the script? I can pay you for doing this.

    Cheer,
    Marcl

Leave a Reply

Your email address will not be published. Required fields are marked *