léonardrodriguez
Menu

How to Show Content Only to Your Facebook Fans

An archived tutorial explaining how Facebook page tabs displayed different content to fans and non-fans.

Fan-only content on a Facebook page
Two versions of a Facebook tab showing different levels of access.

UPDATE: This article dates from 2011. Facebook has changed considerably since then. The follow-up, 2012 Tutorial: Adding an iFrame Tab to Your Facebook Page, updated these instructions and included a downloadable PHP template that displayed different content to fans and non-fans.


As promised at the end of my tutorial on creating a new tab on your Facebook page, here is the next step: showing or hiding specific content to create fan-only pages.

Fan-only content in a Facebook tab

1. Why create fan-only content?

The idea was simple: ‘Like our page to get early access, exclusive content, special offers, or a discount code.’ Whatever the incentive, the goal was to encourage visitors to click Like and grow the page’s audience.

2. What are signed_request and $data?

The previous tutorial explained how to embed an HTML or PHP page in a Facebook iFrame tab. When Facebook loaded that page, it sent encoded information in a POST parameter called signed_request. This let Facebook share information with apps, including page tabs.

We’ll decode that value to retrieve the JSON data we need. JSON – JavaScript Object Notation – is a data format used to exchange information between applications. In this PHP example, the decoded result is an associative array.

Here is the function provided in Facebook’s documentation:

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, '-_', '+/'));
}

That makes decoding the data straightforward:

$data = parse_signed_request($_REQUEST['signed_request'], 'INSERT YOUR APP SECRET HERE');

To make the result easier to follow, here is the structure of $data:

array
(
    [algorithm] => HMAC-SHA256
    [issued_at] => 1234567890
    [page] => Array
        (
            [id] => 12345678910 // ID of the page containing the tab
            [liked] => 1 // Boolean indicating whether the visitor likes the page
            [admin] => 1// Boolean indicating whether the visitor is a page administrator
        )
    [user] => Array
        (
            [country] => fr
            [locale] => fr_FR // The visitor’s language
            [age] => Array
            (
                [min] => 19
            )
        )
)

As the highlighted comments show, the visitor’s fan status wasn’t the only information available. We could also set display conditions based on:

  • The tab the person was viewing.
  • The visitor’s role: page administrator or ordinary visitor.
  • The language used on Facebook.

3. Display different content to fans and non-fans

For this example, we’ll build a ‘Today’s Menu’ tab for a golf course restaurant near Paris. (The original restaurant link is no longer available.) Only fans of the page would be able to read the daily menu.

Visitors who hadn’t liked the page would see a blurred menu image. Fans would see the menu, updated daily and displayed in an iFrame.

Here is the conditional logic:

if(!$data[‘page’][‘liked’]){
// Content for NON-FANS
}else{
// Content for FANS
}

Here’s what it looked like in practice. The tab was published on this Facebook page. Below is the PHP page code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Daily Menu Facebook Page</title>
<meta name=”ROBOTS” content=”NOINDEX,NOFOLLOW”>
</head>

<body>
<div id=”fb-root”></div>
<script src=”http://connect.facebook.net/en_US/all.js”></script>
<script>
FB.init({
appId : ‘YOUR APP ID’,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
<?php
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, '-_', '+/'));
  }
$data = parse_signed_request($_REQUEST['signed_request'], 'YOUR SECRET KEY');
?>
<?php
  if(!$data['page']['liked']){
  EXAMPLE TEXT // content for NON-FANS
  }else{
  EXAMPLE TEXT // content for FANS
  }
  ?>
</body> </html>

Here is the result:

I hope this Facebook tutorial makes the process clear. It is more technical than marketing-focused, but it shows how the feature worked.

HistoryPublication, updates and clarifications
  1. Update
  2. First published

Keep exploring.

All articles ↗

SUGGEST A CORRECTION

Spotted an error?

Tell me which passage needs correcting. Include a source if you can, so I can check it.

We use the information you provide to respond to your message. How your data is used