Determine if a user has liked my page using Facebook PHP SDK?-open source projects facebookarchive/facebook-php-sdk

Old topic, but wanted to weigh in as I have recently learned a lot about how to do this and have had to put it into practice.

Have a look at this link: http://nickring.com/blog/2012/11/facebook-fan-gate-using-php/. It uses the signed_request variable as some of the other responses show, but it shows how it does not require requesting the signed_request variable via $_REQUEST.

The one main thing to remember is that signed_request is only available when the PHP script accessing signed_request is run within Facebook. If you run this script outside of Facebook in a script attempting to use the Facebook API, it will return an empty array.

Here’s an example – the following script will run when you go to this address: https://www.facebook.com/yourFacebookPage/app_xxxxxxxxxxxxxxx with ‘xxxxxxxxxxxxxxx’ being the app ID.

// Check if the user has liked us on Facebook, require the Facebook SDK
require_once('linked/facebook/facebook.php');

// Setup the Config
$config = array(
    'appId' => 'xxxxxxxxxxxxxxxxx',
    'secret' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy',
    'cookie' => true
);

// Create a Facebook SDK instance
$facebook = new Facebook($config);

// Get the signed_request variable that we so desparately need
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];

// Make sure that it worked
if (!empty($signed_request))
{
    // Get the signed_request information
    if ($like_status == 1)
    {
        // Wo0t! Show the FB fan only page stuff here

    }
    else
    {
        // Show the 'Please like us page'
        $page = file_get_contents('pages/facebookLikeUs.html');

        // Finish the page
        echo $page;
        exit();

    } // End if ($like_status == 1) ELSE Clause and IF

} // End if (!empty($signed_request)) IF Clause
else
{
    // Damn, it didn't work. Show an error
}

The above script is the script that is called from the URL set in the Canvas URL in the “App on Facebook” section of the App’s settings. The facebookLikeUs.html page is simply a page asking them to click “Like” to continue. If I’m in a situation that I want them to be redirected back into a website that requires the Facebook like I simply replace the // Wo0t! section with something like this:

    // Wo0t! They're all set, establish some cookies, get a cookie, and redirect back to the PHD program site
    setcookie('fbls', $signed_request['page']['id'] . '-' . $signed_request['user_id'], time() + 300);
    $redirectURL = "http://www.myurl.com/theScriptIWantToReceiveTheUserFromFB.php";

    // Since Facebook really wants to keep us in the page, we need to create a page that will automatically break out of FB
    $page = file_get_contents('pages/facebookRedirectBack.html');

    // Replace some stuff
    $page = str_replace('$redirectURL', $redirectURL, $page);

    // Output the page
    echo $page;
    exit();

With the facebookRedirectBack.html page being this:





window.top.location.href = '$redirectURL';



If you’re looking to have a bit more security on this, you can have the redirecting PHP script write some cookies and attach them to the URL such that the receiving script must compare the cookies and the URL params then delete the cookies after they’ve been read.

Hope this helps as I personally haven’t found any consistent information on this subject.