How to check if a Facebook user liked a page with PHP

Oreo Facebook Page

In my last blog post I showed you how to make a Facebook page welcome tab. Once you have created a page you probably want users to like it or be a fan. One way to encourage this is to show different content depending on if the user is a fan of your page or not. One example of this is Oreo’s Facebook page where users have to like the page to see the content.

One good thing to know before you start is that, according to the Facebook Platform Policies you can’t “incentivize users to Like any Page other than your own site or application”. Please also read the rest of the Facebook Platform Policies so you know what you can and can’t do.

The PHP method

First thing you need to do is to do is to get the current Facebook session. This is done with the getSession() function like this:

$session = $facebook->getSession();

/*If you get a session here it means that a correctly signed session exists (using the Application Secret that only Facebook and the Application know). Note that even though a session is found it doesn’t mean it is still valid. The session could have expired or the user might have logged out. This is handled with the exception model try and catch. If the session isn’t valid anymore the exception will be caught.

Continue like this.*/
if ($session) {
$user = $facebook->getUser(); //get the user id

$page = ' XXXXXXXXXXX'; //replace XXXXXXXXXXX with the page id

try {

$likeID = $facebook->api(array( 'method' => 'fql.query', 'query' => 'SELECT target_id FROM connection WHERE source_id =' . $user . 'AND target_id =' .$page ));

/* Using a fql.query like this checks the current user (source_id) likes (connection) a page (target_id).

Without the AND target_id =' .$page at the end of our query, it would return all the pages and friends the user likes, is a fan of or is a friend with. Since we included the AND we will now only get a return value if the user likes our specific page.

If $likeID is empty the user doesn’t like our page and if $likeID is not empty the user likes it.

You can now show the user different content with a simple if statetment like below.*/

if ( empty($likeID) ) {

// user HAS NOT Liked the page

echo "<h2>Like my page!</h2>";

}

else {

// user HAS Liked the page

echo "<h2>Thank you for liking my page</h2>";

}

}

// if the session has expired, ask the user to login in to Facebook

catch (FacebookApiException $e) {

echo 'Please login to Facebook';

error_log($e);

}

}

?>

That’s it. You now have a Facebook page tab that shows different content whether the user liked the page or not. You probably want to show something more than just “Like my page!” and “Thank you for liking my page”.  You can for example replace the two echoes of h2 tags with an include("notlike.php"); and include("like.php"); like this :

if ( empty($likeID) ) {

// user HAS NOT Liked the page

include("notlike.php")

}

else {

// user HAS Liked the page

include("like.php");

}

And then you just create the two pages notlike.php and like.php add to them the content you want to show.

 

 

 

 

One Response to How to check if a Facebook user liked a page with PHP

  1. james says:

    nice one, ta

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>

By submitting this form, you accept the Mollom privacy policy.