简体   繁体   中英

Facebook Like Button doesn't work

First sorry for my nooby questions, i've checked almost everywhere but i couln't the right answer. There are lot of documents and tutorials but in development its changing. However, my problem is, i have a fan page that has a Tab which is integrated with my app.

I just want to check sessions that users was here and they already liked before or if they are already liked my page i want to update sessions and get new access tokens then redirect them to my fluid canvas page(this is my second question btw. because landing page is in 810px page. after they like pages refreshing thmeself and its opening in fixed area again.)

Do i have to put FB.login button in to my landing page or is it better to ask user permissions when the apps page onloading.

my folder schema is like:

-appfolder
--tab - (landing page in fixed page)
--app - (app page in fluid canvas)
--css
--img
--js
--php-sdk
--index.php (check everything here and redirect in here with login attributes)
--config.php
--fbaccess.php
--fbin.php
--fbout.php

Landing page code :

<?php
    require 'php-sdk/facebook.php';

    $app_id = 'xxx';
    $app_secret = 'xxx';
    // Set
    $app_namespace = 'hangisienmeshur';
    $app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
    $scope = 'email,publish_actions';

    // Init the Facebook SDK
    $facebook = new Facebook(array( 
         'appId'  => $app_id,
         'secret' => $app_secret,
    ));

    // Get the current user
    $user = $facebook->getUser();
?>

    <!DOCTYPE html>
    <html xmlns:fb="http://ogp.me/ns/fb#" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Tropicana Main</title>
    <link href="css/normalize.css" rel="stylesheet" type="text/css" />
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div id="fb-root"></div>

    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
    <script>

      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'xxx', // App ID
          channelUrl : 'http://www.xxx.com/app/xx/channel.php', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });

        // Additional initialization code here
        FB.Event.subscribe('edge.create',
            function(response) {
                window.location.href = 'https://apps.facebook.com/xxx/';
            }
        );
      };

      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
       }(document));


    </script>



    <div id="mainfoto">
    <div id="fb_button" class="myClass rtl">

    <fb:like href="http://www.facebook.com/xxx" width="100" layout="button_count" show_faces="false" send="false"></fb:like>

</div>
</div>

</body>
</html>

Try to avoid the use of sessions in a Facebook app. Facebook loads your webpage in an iframe, which some browsers (Safari in particular) wont work properly with, due to security restrictions.

Facebook recommends to wait with permissions until they're become necessary. You could request the permissions on canvas-load. Best usecase would be to wait for the user to do an action which requires their information (for example, clicks a "Signup" button).

For your flow to work, I would suggest this approach.

On the landingpage, use signed request to tell if the user has liked your page.

$signed = parse_signed_request($_REQUEST['signed_request'], 'YOUR-APP-SECRET');
if ($signed['page']['liked'] == 1) {
    $fan = true;
} else {
    $fan = false;
}

if ($fan)
{
    echo "window.location.href = 'https://apps.facebook.com/xxx/';";
}
else
{
     echo "<p>Please like the page.</p>";
}

( source )

This will redirect users who has liked your page, to the canvas page. Users who hasn't, will see the message "Please like the page".

You dont need to subscribe to 'edge.create', as a click on the "Like" button, will force a page reload and your code will then do the redirect since the user now likes the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM