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 } ?>

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'.'?sk=app_'.$app_id;
@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.
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?
I get the message ‘Click Like To View Content’ even after the page is ‘Liked’
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.
I tried this code, but I get the following error:
Parse error: syntax error, unexpected ‘{‘ in MYAPP.php on line 41
Any suggestions?
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
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
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
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?
Hi, I keep getting the same problem with Kieran . It only shows “Click like to view content”.