简体   繁体   English

Facebook登录链接有效,但不是登录按钮,任何想法为什么?

[英]Facebook login link works but not the login button, any ideas why?

I'm trying to create a php page where the user can login and then like my page. 我正在尝试创建一个用户可以登录然后喜欢我的页面的php页面。 If I use the login_url in a block, it works great. 如果我在块中使用login_url,它的效果很好。

However I'd like to use the official facebook login button, particularly because it allows the locale to be passed in, and then it will be localized to the right language.... and it looks nice! 但是,我想使用官方的Facebook登录按钮,特别是因为它允许传入区域设置,然后它将被本地化为正确的语言....它看起来不错!

Doing some research, it seems as though the login state in php is not in synch with the login state in js, but I have only seen bits and pieces of code (most of it with obsolete versions of the api). 做一些研究,似乎php中的登录状态与js中的登录状态不同步,但我只看到了一些代码(大部分都是过时的api版本)。

Can anyone please point me in the right direction as to what I need to do here to make the facebook login button do the same thing as the $login_url? 任何人都可以指出我正确的方向,我需要做什么,使Facebook登录按钮与$ login_url做同样的事情?

Right now I observe the following behavior: 现在我发现以下行为:

  1. I open up the page and the Login button and link is visible because the php code thinks the user is not logged in 我打开页面,登录按钮和链接是可见的,因为php代码认为用户没有登录
  2. I click the Login Button and log in 我单击“登录”按钮并登录
  3. The php code still thinks the user is not logged in, so it still shows the login button php代码仍然认为用户没有登录,因此它仍然显示登录按钮
  4. Only until I click the Login link do I get logged in correctly and the login button disappears on reload. 只有在我单击“登录” 链接之后才能正确登录并且重新加载时登录按钮会消失。

Right now clicking the button just reloads the page and doesn't even ask the user to login. 现在点击按钮只是重新加载页面,甚至不要求用户登录。 And if the user is logged in using the login-button, the php part of the page thinks the user is not logged in... 如果用户使用登录按钮登录,则页面的php部分认为用户未登录...

<?php 

require("facebook/facebook.php");

$locale = "en_US"; // TODO

$connect_facebook_url = "'//connect.facebook.net/" . $locale . "/all.js#xfbml=1&appId=XXXXXXXXXXXXXXXXXXXXXXX'";

$facebook = new Facebook(array(
  "appId"  => "XXXXXXXXXXXXXX",
  "secret" => "XXXXXXXXXXXXXXXXX",
  "cookie" => true,
));

$access_token = $facebook->getAccessToken(); 

// Get User ID
$user_id = $facebook->getUser();

if($user_id) 
{
  // We have a user ID, so probably a logged in user.
  // If not, we'll get an exception, which we handle below.
  try 
  {
    $user_profile = $facebook->api('/me','GET');

    $likes = $facebook->api(array( 
      'method' => 'fql.query',
      'query' => 'SELECT page_id FROM page_fan WHERE uid = "'
                 . $user_profile['id']
                 . '" AND page_id="XXXXXXXXXXXXX"', )); 

  } 
  catch(FacebookApiException $e) 
  {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $login_url = $facebook->getLoginUrl(); 
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} 
else 
{
  // No user, print a link for the user to login
  $login_url = $facebook->getLoginUrl();
}

if(count($likes) > 0) 
{
  $likes_result = "User likes this page"; 
}
else 
{
  $likes_result = "User doesnt like this page";
}

if($login_url)
{
  $login_string = '<a href="' . $login_url . '">' . 'IDST_MENUITEM_SIGN_IN' . '</a>';
}

?>

<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
<div id="fb-root"></div>
<script>
  (function(d, s, id) 
  {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = 
    <?php echo $connect_facebook_url; ?>
    ;
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
</script>

<body bgcolor="#000000">

<div align="center">
  <table>
  <tbody>
  <tr>
    <td>    
      <img src="http://www.myapp.com/wp-content/uploads/2012/05/MyApp-logo.png"/>
    </td>
    <td style="align:left; vertical-align:middle">
      <p style="font-family:segoe ui;color:white;font-size:40px;">MyApp</p>
    </td>
  </tr>
  </tbody></table>
</div>

<p>&nbsp<p>

<div align="center" style="font-family:segoe ui;color:white;font-size:20px;">
  <?php if ($login_string) : ?>
    <div class="fb-login-button" size="xlarge" data-width="200"></div>
  <?php else: ?>
    <div class="fb-like" 
         data-href="https://www.facebook.com/MyApp" 
         data-width="200" 
         data-colorscheme="dark" 
         data-show-faces="true" 
         data-send="true">
    </div>
  <?php endif; ?>

  <br>

  <p>
    <?php echo $likes_result; ?>
  </p>
  <p>
    <?php echo 'Please <a href="' . $login_url . '">login.</a>'; ?>
  </p>
</div>

</body>
</html>

There You Go. 你去吧。 Hope this helps 希望这可以帮助

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <style type="text/css">
      div#container_notlike, div#container_like {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'YOUR_APP_ID', // App ID
          channelUrl : 'http(s)://YOUR_APP_DOMAIN/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });

        FB.getLoginStatus(function(response) {
          var page_id = "YOUR_PAGE_ID";
          if (response && response.authResponse) {
            var user_id = response.authResponse.userID;
            var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
            FB.Data.query(fql_query).wait(function(rows) {
              if (rows.length == 1 && rows[0].uid == user_id) {
                console.log("LIKE");
                $('#container_like').show();
              } else {
                console.log("NO LIKEY");
                $('#container_notlike').show();
              }
            });
          } else {
            FB.login(function(response) {
              if (response && response.authResponse) {
                var user_id = response.authResponse.userID;
                var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
                FB.Data.query(fql_query).wait(function(rows) {
                  if (rows.length == 1 && rows[0].uid == user_id) {
                    console.log("LIKE");
                    $('#container_like').show();
                  } else {
                    console.log("NO LIKEY");
                    $('#container_notlike').show();
                  }
                });
              } else {
                console.log("NO LIKEY");
                $('#container_notlike').show();
              }
            }, {scope: 'user_likes'});
          }
        });
      };

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

    <div id="container_notlike">
      YOU DON'T LIKE ME :(
    </div>

    <div id="container_like">
      YOU LIKE ME :)
    </div>

  </body>
</html>

Replace the "YOUR_PAGE_ID" with your page id. 将“YOUR_PAGE_ID”替换为您的网页ID。

if (rows.length == 1 && rows[0].uid == user_id) {
                        console.log("LIKE");
                        $('#container_like').show();
                        //window.location=YOURPHP.php    

If you use window.location you will be redirected to YOURPHP.pho, if you dont want that 如果您使用window.location,您将被重定向到YOURPHP.pho,如果您不想要它

use ajax here in the code.

$.post("database.php",{userid: user_id, likes:1"},
  function(data,status){
  console.log(data);
console.log("LIKE");
  $('#container_like').show();
  posttofacebook(caption);
  document.getElementById("captionresult").innerHTML=data;
}); 
                      }

If you have any suggesstion queries or doubts on this.. Feel free to contact. 如果您对此有任何疑问或疑问,请随时联系。

Try using session 尝试使用会话

if($user_id) {
    try {
        $user_profile = $facebook->api('/me','GET');
        $_SESSION['user'] = $user_profile['id'];
    }
}

Then on your html page, you can add: 然后在您的html页面上,您可以添加:

<?php if(isSet($_SESSION['user'])) { ?>
         Your html content here
    <?php } ?>

In case session not started, just add session_start() at the begging of page. 如果会话未启动,只需在页面的乞讨处添加session_start()。 Hope this will help 希望这会有所帮助

I had the same problem and solved it with a simple one line setting: 我有同样的问题,并通过简单的一行设置解决了它:

in FB.init you have no xbmfl default value set, so it's defaulting to false and thus not rendering any social icons. 在FB.init中,您没有设置xbmfl默认值,因此它默认为false,因此不会呈现任何社交图标。 Simply add 只需添加

$facebook = new Facebook(array(
  "appId"  => "XXXXXXXXXXXXXX",
  "secret" => "XXXXXXXXXXXXXXXXX",
  "cookie" => true,
  "xbmfl" => true
));

According to the facebook initialization documentation : XBMFL: Determines whether XFBML tags used by social plugins are parsed, and therefore whether the plugins are rendered or not. 根据facebook初始化文档 :XBMFL:确定社交插件使用的XFBML标记是否被解析,因此是否呈现插件。 Defaults to false. 默认为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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