如何从Facebook应用程序注销用户,但保持从Facebook登录

Ali*_*ias 9 facebook logout facebook-javascript-sdk facebook-php-sdk

我已经尝试了PHP SDK(v.3.1.1)和当前建议的Javascript SDK:https: //developers.facebook.com/docs/guides/web/

现在当我试图注销时,我已经尝试了FB.logout()(对于js)和$ facebook-> getLogoutUrl();

作为两个清楚状态的文档,这些方法将用户从应用程序以及他们的facebook会话中记录下来.

但我只需要将用户从facebook应用程序(测试站点)中注销.

我已经尝试将用户从测试站点中删除,忽略了facebook方面.但在这种情况下,当用户再次单击登录按钮时,登录流程(facebook身份验证和重定向)不会发生.

我也试过:(正如以前未解决的问题所建议的那样)

$facebook->destroySession();

unset($_SESSION['fb_' . sfConfig::get('app_fb_config_id') . '_code']);
unset($_SESSION['fb_' . sfConfig::get('app_fb_config_id') . '_access_token']);
unset($_SESSION['fb_' . sfConfig::get('app_fb_config_id') . '_user_id']);    
Run Code Online (Sandbox Code Playgroud)

但是,当重定向到登录页面时,$ facebook-> getUser()仍然会检索用户.

注意:根据文档示例,我使用php sdk登录用户到我的测试站点,以及js sdk,以呈现和方便facebook登录按钮.

附加:

我使用的身份验证基本上是文档建议的:

<?php

define('YOUR_APP_ID', 'YOUR APP ID');

//uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => YOUR_APP_ID,
  'secret' => 'YOUR APP SECRET',
));

$userId = $facebook->getUser();

?>

<html>
  <body>
    <?php if ($userId) { 
      $userInfo = $facebook->api('/' + $userId); ?>
      Welcome <?= $userInfo['name'] ?>
    <?php } else { ?>
    <div id="fb-root"></div>
    <fb:login-button></fb:login-button>
    <?php } ?>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '<?= YOUR_APP_ID ?>',
          status     : true, 
          cookie     : true,
          xfbml      : true,
          oauth      : true,
        });

        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
      };

      (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>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Nit*_*mer 5

拿你的代码,稍微修改一下:

<?php
    define('YOUR_APP_ID', 'YOUR APP ID');

    //uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
    require 'facebook.php';

    $facebook = new Facebook(array(
      'appId'  => YOUR_APP_ID,
      'secret' => 'YOUR APP SECRET',
    ));

    session_start();

    $userId = $facebook->getUser();
    if ($userId && !isset($_SESSION['fbdata'])) {
        $_SESSION['fbdata'] = array("userid" => $userId);
    }
?>

<html>
    <body>
    <?php if ($userId) {
        $userInfo = $facebook->api('/' + $userId); ?>
        Welcome <?= $userInfo['name'] ?>
    <?php } else { ?>
        <div id="fb-root"></div>
        <fb:login-button></fb:login-button>
    <?php } ?>
        <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({
                    appId      : '<?= YOUR_APP_ID ?>',
                    status     : true, 
                    cookie     : true,
                    xfbml      : true,
                    oauth      : true,
                });

                FB.Event.subscribe('auth.login', function(response) {
                    window.location.reload();
                });
            };

            (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>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我刚刚将用户数据保存到会话中.现在,如果您使用此方法,当用户进入您的页面时,您应该检查他是否已经有一个会话,如果是这样,一切都很好,不需要对他进行身份验证.

如果您想要将用户从应用程序中注销,而不是从Facebook中注销,只需销毁该会话:

session_start(); 
session_destroy();
Run Code Online (Sandbox Code Playgroud)

这应该删除您为用户保存的所有数据,下次他访问您的页面时,您可以重新开始.

我不是一个php程序员,我在这里使用的所有php都来自我(非常)有限的知识和我在周围找到的东西.