简体   繁体   English

使用PHP和JS SDK的Facebook注销

[英]Facebook Logout with PHP and JS SDK

I have the following code (with my facebook.php correctly added in, appId and appSecret are also correct): 我有以下代码(正确添加了我的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>

My code always outputs Welcom myemail@email.com . 我的代码始终输出myemail@email.com How do I get it to logout correctly so that I can test it to login again? 我如何使其正确注销,以便可以对其进行重新登录进行测试?

Delete cookies, session, etc. Then with the facebook API do something like this: 删除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);

If you want the user to go back to a specific page just change the 'next' value to whatever page you want them to go back to. 如果您希望用户返回到特定页面,只需将“下一个”值更改为您希望他们返回的任何页面。

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

@Somnath Muluk , it seems this code will clear the session, so not just the fb app will logout, the facebook website loginstatus also will be clear. @Somnath Muluk,似乎这段代码将清除会话,因此不仅fb应用程序将注销,Facebook网站的loginstatus也将清除。

But i think the app-logout-option will just let the app logout of fb. 但是我认为app-logout-option只会让fb退出应用程序。 when I continue to visit my fb feeds it should work without let me do the login option again 当我继续访问我的fb feed时,它应该可以正常工作,而无需让我再次执行登录选项

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

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