登录按钮登录后不消失


Login button not disappearing after login

我刚刚开始学习php,我正在开发一个Facebook应用程序。我现在面临的问题是,登录按钮仍然在屏幕上,即使用户已经访问了他的帐户。有人能解释一下吗?下面是我的代码:

<?php if (isset($user_profile)) {
  ?>
         user logged in
<?php }  else { ?>
  <div class="fb-login-button" data-scope="email"></div>
<?php } ?>

我包含了一个文件,其中$user_profile被定义为$user_profile = $facebook->api('/me');

使用var_dump($user_profile)命令测试$user_profile的输出。您还可以在var_dump之后添加exit命令来暂停执行。从您的代码示例中,else块正在运行,这意味着您的$user_profile变量存在问题。

是否启用了php错误日志记录?航海日志上写了什么?

如果不查看utils.php

,很难帮助您进行调试。

isset是一个函数,如果创建了一个变量并且有值,则返回true。

根据您的帐户,这意味着您的$facebook->api('/me')返回null或该行代码根本没有运行。

我不能确定你的代码出了什么问题,因为没有看到你在做什么,我只能猜测。

不管怎样,既然你在使用PHP,何不一开始就省略登录按钮呢?只要在用户进入应用(如果他们没有登录Facebook)时提示用户登录,如果收到Facebook会话,向他们展示实际的应用,你就已经有了用户的个人资料信息。听起来怎么样?

以下是我在Facebook应用程序中经常做的事情。你可以根据自己的需要进行调整。

config。

<?php
//set facebook application id, secret key and api key here
    $fbconfig['appid'] = "xxxxxxxxxxxxxxxx";
    $fbconfig['secret'] = "xxxxxxxxxxxxxxxxxxxxxxxxx";
    $protocol = "http";
    if(isset($_SERVER['HTTPS']))
    {
        if($_SERVER['HTTPS'])
        {
            $protocol = "https";
        }
    }
//set application urls here
    $domainName = $_SERVER['HTTP_HOST'];
    $appNamespace = "[APP_NAMESPACE]";
    $appFolder = "[WEB_FOLDER_NAME]";
    $fbconfig['appBaseUrl'] =   $protocol."://apps.facebook.com/".$appNamespace;
    $fbconfig['baseUrl']    =   $protocol."://" . $domainName . "/" . $appFolder;
    $uid = null; // facebook user id
    $me = null; // JSON object containing the basic user information
    $redirect_url = $fbconfig['baseUrl']."/redirect.php";
    $cancel_url = $protocol."://www.facebook.com";
    $allow_url = $fbconfig['appBaseUrl'];
    $profileURL = $protocol."://www.facebook.com/profile.php?id=";
?>

redirect.php

<?php
include_once "config.php";
if (isset($_REQUEST["error"]))
{
    ?>
    <script type="text/javascript">
        top.location.href = "<?=$cancel_url?>";
    </script>
    <?php
}
else
{
    ?>
    <script type="text/javascript">
        top.location.href = "<?=$allow_url?>";
    </script>
    <?php
}

?>

fbmain.php

<?php
include_once "../facebook_sdk/facebook.php";
include_once "config.php";

    $uid = null; // facebook user id
    $me = null; // JSON object containing the basic user information
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret'],
  'cookie' => true,
));
$signedRequest = $facebook->getSignedRequest();
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$logoutUrl = $facebook->getLogoutUrl();
$loginUrl = $facebook->getLoginUrl(
                array(
                    //state what are the required perms
                    'redirect_uri' => $redirect_url,
                    'scope' => 'email, user_likes, user_status, friends_likes, friends_status',
                )
            );
// Session based API call.
if ($user) {
  try {
    $me = $facebook->api('/me');
    if($me)
    {
        $uid = $me['id']; 
        $_SESSION['fbId'] = $me['id'];
    }
  } 
  catch (FacebookApiException $e) {
    error_log($e);   
  }
}
else {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}
function outputData($d){
    echo '<pre>';
    print_r($d);
    echo '</pre>';
}
?>

之后,我只需要在任何需要facebook会话的页面中包含"fbmain.php"。我通常会把include放在我的"index.php"中。

index . php

<?php
include_once "fbmain.php";
?>
<html>
    <body id="my_body">
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Canvas.setAutoGrow();
        FB.Canvas.scrollTo(0,0);
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
    <?php
        if ($me)
        {
            echo "Facebook ID: ".$me['id']. "<br />"; 
            echo "Facebook Name: ".$me['name'];
        }
    ?>
    </body>
</html>

站点中的后续页面将使用会话变量使用Javascript SDK进行api调用,而不是再次经历整个身份验证流程。