Making A Like-Gate For A Facebook Fan Page

Want to know how to make a fan page tab that shows one thing to users who like the page, and something different to those who do not? This is called a “like gate” and is pretty easy to implement using PHP on the server side to do the hard part. Be sure to download a copy of the facebook PHP SDK. The file is required by the script below.

*Update* Source is now avaiable from github.

First, create a new facebook app. You will need the appID and App Secret to customize the code below.

Then make a fan page. Add you app to the fan page. You can do this by navigating the the application description page and clicking “Add to my page”. A list of pages owned by you will show up. Click the page you want to add the app to.

Now the app is in a tab. Copy that url. You’ll need that too. That will be your $loginNextPage – the page the user will be directed to after clicking like – which we want to be our tab again.

Finally, use the code below to detect whether the user likes the fan page or not.

<?php
 
require 'facebook.php';
 
$app_id = "YOUR APP ID";
$app_secret = "YOUR APP SECRET";
 
$loginNextPage = 'PATH TO YOUR FAN PAGE ON FACEBOOK'.'?sk=app_'.$app_id;
 
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));
 
$signed_request = $facebook->getSignedRequest();
 
$like_status = $signed_request["page"]["liked"];
 
 
if ($like_status) {
	// FOR FANS
	$session = $facebook->getSession();
    $loginUrl = $facebook->getLoginUrl(
            array(
            'canvas'    => 1,
            'fbconnect' => 0,
			'next' => $loginNextPage
            /*'req_perms' => 'publish_stream,photo_upload,user_photos,user_photo_video_tags'			*/
            )
    );
 
    $fbme = null;
 
	if (!$session) {
		echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";	
        exit;	
	}
	else {
 
		try {
            $access_token = $facebook->getAccessToken();
			$vars = "access_token=$access_token&pathToServer=$pathToServer&appName=$appName";
        } catch (FacebookApiException $e) {
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">	
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
</head>
 <body>
 <h1>You Liked The Page</h1>
</body>
</html>
<?php
	}	
}
else {
	// FOR NON FANS	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">	
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
  </head>
  <body>
  <h1>You Don't Like the Page</h1>
	</body>
</html>
<?php
 
}
 
?>

About The Plastic Sturgeon

This is my site. It gets pretty biographical on the About page.
This entry was posted in Tutorials. Bookmark the permalink.

12 Responses to Making A Like-Gate For A Facebook Fan Page

  1. Greg says:

    Hey, thanks so much for this. I installed everything but I can’t figure out what I’m doing wrong. I updated the top portion of the .php code

    <?php

    require 'facebook.php';

    $app_id = "REMOVED";
    $app_secret = "REMOVED";
    $loginNextPage = 'http://www.facebook.com/pages/Snakesbee-Studios/233994129949445?sk=app_293384570687126&#039;.'?sk=app_'.$app_id;

  2. @greg – what is your error message? Did you get the Facebook.php file? Also – I removed your app ID and secret key from your message post, because, well that secret key is meant to be secret.

  3. Karim says:

    I tried your method but it won’t detect the “like” ie it always goes to the non-liked page. Have you run into this issue?

  4. Kevin says:

    I get the message ‘Click Like To View Content’ even after the page is ‘Liked’

  5. I checked the code, and there was some sort of error. Also, I noticed I was using short PHP tags which will cause errors on some servers. I just updated the code above, and I tested it on a facebook fanpage tab. It works. Please update your code and see if this solves it for you. Sorry for the inconvenience.

  6. Savannah says:

    I tried this code, but I get the following error:
    Parse error: syntax error, unexpected ‘{‘ in MYAPP.php on line 41

    Any suggestions?

  7. Nick says:

    i end up with this error:
    does anybody have an idea how to fix this?

    Warning: Unexpected character in input: ” (ASCII=92) state=1 in /home/shoeg/domains/shoegazer.nl/public_html/fb/index.php on line 12

    Warning: Unexpected character in input: ” (ASCII=92) state=1 in /home/shoeg/domains/shoegazer.nl/public_html/fb/index.php on line 12

    Parse error: syntax error, unexpected T_STRING in /home/shoeg/domains/shoegazer.nl/public_html/fb/index.php on line 12

  8. It seems like copyingand pasting the file was causing some problems for people, with brackets and quotes and what not getting scrambled. So I put the files on github. Go get the sources here: https://github.com/zfoley/Facebook-Like-Gate-Example

  9. Kieran says:

    Hi,

    I keep getting the ‘Click Like To View Content’ even after the page is ‘Liked,’ and this is after using the code from github. Any suggestions why? Thanks

  10. Susan says:

    I installed this on 12/15 and everything was working great – in testing today I get an error code when ‘liking’ the page. **An error occurred with IQnection Fan Page. Please try again later.**
    Any ideas? Do you know if FB made updates?

  11. Johann says:

    Hi, I keep getting the same problem with Kieran . It only shows “Click like to view content”.

  12. Shaun Badenhorst says:

    Your code on github doesn’t make sense, you have 2 else statements after each other. NUTS!!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>