PHP Check If User Like Facebook Page
Posted by in PHP February 20, 2012 2 Comments

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 PHP Check If User Like Facebook Page Source Code, you need to change some settings (app id, app secret, etc) in the config.php file.
+ View our demonstration page.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.facebook.com/faruk.ahammed.73 Faruk Ahammed

    This articfle

  • Nickto30

    H , 

    i get the error message ”
    An error occurred with Marsino_olympic. Please try again later.”