There are few ways that determine whether a particular Facebook user is a FAN of a particular Facebook Page. Currently, I’m querying the Facebook page_fan table by using FQL to check and restrict non fan to access my demonstration pages. Meaning they have to like our Facebook Fan Page before checking out some information.
The following source code uses the Facebook API to gets logged in Facebook user information, checks permissions (and redirect if needed), checks whether a user is a fan and displays result.
Query Facebook page_fan table using FQL
<?php require 'src/facebook.php'; require 'config.php'; $facebook = new Facebook(array( 'appId' => $appId, 'secret' => $secret, )); $user_id = $facebook->getUser(); if($user_id == 0 || $user_id == "") { $login_url = $facebook->getLoginUrl(array( 'redirect_uri' => $return_url, 'scope' => "email,publish_stream,user_hometown,user_location,user_photos,friends_photos, user_photo_video_tags,friends_photo_video_tags,user_videos,video_upload,friends_videos")); echo "<script type='text/javascript'>top.location.href = '$login_url';</script>"; exit(); } $result = $facebook->api(array( "method" => "fql.query", "query" => "SELECT uid FROM page_fan WHERE uid=$user_id AND page_id=$page_id" )); if(count($result)) echo "You liked this Page!" . "<br>"; else echo "You haven't liked this Page yet!" . "<br>"; ?> |
+ [download id=”8″ format=”1″], you need to change some settings (app id, app secret, etc) in the config.php file.
+ View our demonstration page.