如何从Facebook应用程序注销用户,但保持登录 [英] How to log out user from facebook application, but maintain login from facebook

查看:147
本文介绍了如何从Facebook应用程序注销用户,但保持登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了PHP SDK(v.3.1.1)和当前Javascript SDK
,如下所示: https://developers.facebook.com/docs/guides/web/

I have tried the PHP SDK (v.3.1.1), and the current Javascript SDK as suggested here : https://developers.facebook.com/docs/guides/web/

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

now when trying to log out I have tried both FB.logout() ( for js ) and $facebook->getLogoutUrl() ;

作为清除状态的文档,这些方法
将用户登录到应用程序以及他们的Facebook会话中。

as the documentation for both clearly state, these methods log the user out of the application as well as their facebook session.

但是,我只需要将用户登录到Facebook应用程序(测试站点)

But I only need to log the user out of the facebook application ( the test site ).

我尝试将用户登录到我的测试网站,忽略了Facebook
方面。但是在这种情况下,当用户再次单击登录按钮时,
登录流程(Facebook身份验证和重定向)不会发生。

I have tried logging the user out of my test site, ignoring the facebook aspect. But in this case, when the user clicks the login button again, the login flow ( facebook authentication and redirect ) does not happen.

我也尝试过: (如以前未解决的问题所提出的)

I also tried : ( as suggested by previous unresolved questions)

$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']);    

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

however, when redirecting to the login page, $facebook->getUser() still retrieves the user.

注意:
根据文档示例,我使用php sdk将用户登录到我的测试站点
和js sdk,提供和方便的Facebook登录按钮。

note : as per documentation example, I am using php sdk to login the user to my test site, and the js sdk, to render and facilitate the facebook login button.

其他:

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

the authentication i use is basically what documentation suggests :

<?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>


推荐答案

取代码,修改一下: / p>

Taking your code, and modifying just a bit:

<?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>

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

I just added the saving of the user data into the session. Now, if you use this method, when the user get's into your page you should check if he already has a session, if so all is good no need to authenticate him.

当您想将用户登录到应用程序之外,而不是从Facebook中登录时,只需销毁该会话:

When you want to log the user out of your app, but not out of facebook, just destroy that session:

session_start(); 
session_destroy();

应该删除您为用户保存的所有数据,下次访问您的页面时,您可以启动新鲜与他。

That should remove all saved data you have for the user, next time he visits your page you can start fresh with him.

我不是一个PHP程序员,我在这里使用的所有PHP都是来自我(非常)有限的知识和我发现的。

I'm not a php programmer, and all of the php I used here is from my (very) limited knowledge and what I've found around.

这篇关于如何从Facebook应用程序注销用户,但保持登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆