The problem:
You have some awesome content on your facebook page, but you want visitors to like your page before they can read it, aka installing a fangate! No worries, PHP is our friend and this example can be set-up in under five minutes! You can do this with javascript too, but we over at Murten Saerbi choose the PHP way.


Woah… wait! What’s a fangate?
A fangate is actually just a page that checks whether a certain visitor is a fan of your page or not. Therefore this script will only work when you load it in a page. You can do this by creating a new app over on developers.facebook.com and adding the app to your page. The custom pages on Facebook are actually *just* HTML/PHP/… pages that Facebook loads into an iframe.

Like that it looks the content of your website is actually on Facebook inside a custom page tab.


The solution:
First you will need to download the php sdk file, you can do this over at GitHub. Next step is to actually create a php file that you can use as the fangate page. Open up the php tags and start with the base: requiring the file we just downloaded (don’t forget to upload!) and filling in the app id and the app secret. You can find these variables on your application setting screen on developers.facebook.com. Your file should look a bit like this:

<?php
 
require 'facebook.php';
 
$app_id = "your_app_id_here"; 
$app_secret = "your_app_secret_here";
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));

After this all we really need is a parameter that tells us if a visitor likes the page he’s viewing or not. You can find this in the signed_request object, a Facebook object that holds various information about the page and the user. Get that object and read out the page_liked parameter:

<?php
$signed_request = $facebook->getSignedRequest();
 
$like_status = $signed_request["page"]["liked"];

When we have the like status inside a variable the rest is easy, a simple if statement will give us the ability to feed the visitor two content pages: one if the user doesn’t like our page yet and one if the user does like our page. That’s about it: easy peasy lemon squeezy! Just don’t forget to close the php code after the if and you’re all set:

if ($like_status) {
echo "<div style='margin-top:0px; position: absolute; top:0px;left:0px; width:520px; height:750px;'>
    <iframe src='HTTP://_THE_PAGE_THAT_NEEDS_TO_BE_SHOWN_WHEN_PEOPLE_LIKE' width='520' height='751' frameborder='0' scrolling='no'></iframe>
</div>";
} else {
echo "<div style='margin-top:0px; position: absolute; top:0px;left:0px; width:520px; height:750px;'>
  <iframe src='HTTP://_THE_PAGE_THAT_NEEDS_TO_BE_SHOWN_WHEN_PEOPLE_DONT_LIKE' width='520' height='751' frameborder='0' scrolling='no'></iframe>
</div>";
}
 
?>

Online you should now have atleast four files:

- the facebook.php file
- the fangate php file (index.php for instance)
- the file that people see when they’re not a fan yet
- the file that people see when they’re a fan

It’s that easy, really: so open up your favourite texteditor and start fan-gating away!