php - How can I make a facebook app work only on my page?

988

I wrote an app that works as a page tab, but I don't want others to use it. Is it possible to make the app work only on my page?

Also, If it is possible to redirect the user to my page if he accessed the app through another page?

722

Answer

Solution:

This is somewhat crude but should work. This will check if the current page ID is your page ID and if not display an error message.

<?php
    // Include the Facebook PHP SDK
    require('includes/fbSDK/facebook.php');

    // Enter your app details
    $app_id = "xxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxx";
    $facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
    ));

    $signed_request = $facebook->getSignedRequest();

    // Get the curren page ID
    $page_id = $signed_request["page"]["id"];

    // Enter your page ID
    $mypageID = '123456789';

    if ($page_id == $mypageID):
        include('/your/app/files.php');
    elseif ($page_id != $mypageID):
        echo "This app only works on a single page!";
    endif;
?>

Here are some examples of what you can check for:

$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];

You can for example use this method to require someone to like a page before you show content.

Hope this helps!

People are also looking for solutions to the problem: Merge two arrays by key but mantain the key of the first in PHP, array_combine fails

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.