使用PHP和JS SDK注销Facebook

Facebook Logout with PHP and JS SDK

本文关键字:注销 Facebook SDK JS PHP 使用      更新时间:2023-09-26

我有以下代码(正确添加了我的facebook.phpappIdappSecret也是正确的):

 <?php
 define('APP_ID', 'XXX');
 $facebook = new Facebook(array(
     'appId'  => APP_ID,
     'secret' => 'XXX',
));
$userId = $facebook->getUser();
?>
 <html>
  <body>
 <div id="fb-root"></div>
<?php if ($userId) { 
  $userInfo = $facebook->api('/' + $userId); ?>
  Welcome <?= $userInfo['email'] ?> <fb:logout-button></fb:logout-button>
<?php } else { ?>
<div id="fb-root"></div>
<fb:login-button></fb:login-button>
<?php } ?>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '<?= APP_ID ?>',
      status     : true, 
      cookie     : true,
      xfbml      : true,
      oauth      : true,
    });
FB.logout(function()
{
    top.location.href = 'http://www.google.com'
});
  };
  (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>

我的代码总是输出Welcom myemail@email.com。我如何让它正确注销,以便测试它再次登录?

删除cookie、会话等。然后使用facebook API执行以下操作:

        $logoutUrl = $facebook->getLogoutUrl(array('next' => $_SERVER['HTTP_REFERER'], 'session_key' => $fb_session['session_key']));
        $fb_session = $facebook->setSession(null);
        header('Location:' . $logoutUrl);

如果你想让用户返回到一个特定的页面,只需将"下一个"值更改为你想让他们返回的任何页面。

$fb_session = $facebook->setSession(null);

@Somnath Muluk,似乎这个代码会清除会话,所以不仅fb应用程序会注销,脸书网站loginstatus也会被清除。

但我认为应用程序注销选项只会让应用程序注销fb。当我继续访问我的fb提要时,它应该可以工作,而不需要让我再次执行登录选项