Blog

check-facebook-fan-page-like-in-phpcheck-facebook-fan-page-like-in-php

How to Check if a User is Following Your Facebook Page in PHP

Social media optimization is really essential these days and awarding your site users some points or badges for liking your Facebook fan page is a nice trick to swiftly increase your Facebook fan base. In order to award a user some social bonus, we first need to check whether that user has actually liked the Facebook fan page or not. In order to check this we need the user to login into its Facebook account and allocate all the necessary permissions that our Facebook app will ask (OAuth 2.0).

In case you are NOT OK with user giving the permissions you can skip this tutorial as this tutorial specifically requires OAuth permissions from the users. And, if you are OK with OAuth permissions and your site happens to be a PHP based website, follow this step by step tutorial to easily detect whether a user has liked your fan page or not:

 

Step: 1

Create a new Facebook app here. Enter the URL of your site in Settings->Platform->Website->Site URL and note down the ‘App ID’ and ‘App Secret’ as we will be using these in step 5!

 

Step: 2

Download Facebook PHP SDK from here. Extract the files and place it in the same folder with the file in which you will paste the code given in the step 5 below!

 

Step: 3

Get your Facebook Fan Page ID using this tool.

 

Step: 4 (Optional)

Generate Facebook like box code for your page from here. You can display this like box if a user hasn’t already liked the page.

Step: 5

After 1, 2, 3 and 4 steps are complete, you can check if user has liked a page or not with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!--?php require('facebook.php');
    $config = array(
         'appId' =?--> 'your facebook app id',
         'secret' => 'your facebook app secret code',
        'allowSignedRequest' => false
    );
    $facebook = new Facebook($config);
    $user_id = $facebook->getUser();
    if (isset($user_id)) {
        try {          
            $likes = $facebook->api('/me/likes/your_facebook_page_id_here', 'GET');            
            if (!empty($likes['data'])) // if user has liked the page then $likes['data'] wont be empty otherwise it will be empty
            {
                echo 'Thank you for liking our fan page!';         
                // you can write some custom code here to award users some points or some badge        
            }
           else {
                echo 'You have not liked our fan page! Like it now:';
                ?>                  
                <iframe src="//www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Farkhitech&width&height=290&colorscheme=light&show_faces=true&header=true&stream=false&show_border=true&appId=1392604484339363" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:290px;" allowtransparency="true"></iframe> //replace this with your own Facebook like box code
                <!--?php }
        } catch (FacebookApiException $e) {
            $login_url = $facebook-?-->getLoginUrl();
            echo '<a href="' . $login_url . '">Please click here to login into your Facebook account.</a>';
            error_log($e->getType());
            error_log($e->getMessage());
        }
    } else {
        $login_url = $facebook->getLoginUrl();
        echo '<a href="' . $login_url . '">Please lick here to login into your Facebook account</a>';
    }
    ?>

 

Output

The user will click on the “Please click here to login into your Facebook account.” text which will redirect it to our Facebook app’s (created in step 1) permissions page, once the user grants the permissions to the app, the code will fetch user’s data and will display the likebox if user hasn’t liked the fan page.

If the user has already liked the page then it will display the thank you message (and will run some custom code included in this section to award some points/badge to the user).

 

Prevent Cheating

In order to award points only once (so that users cant get points again and again by liking and unliking the page), you can attach a Boolean field (with a default value of 0) with all user accounts which you can turn to 1 once the bonus points have been awarded. An additional conditional check at line 14 in the code given above like “if (!empty($likes[‘data’] && $userfield != 1 ))” will do the trick.

You can also print a message like “You already got the bonus for liking the page!” for users that have already got the bonus points by adding another if statement like this “if (!empty($likes[‘data’] && $custom_user_field == 1 ))”.

 

Feel free to discuss any issues that you may face in the comments section below!