facebook注销只是从网站注销而不是从Facebook注销? [英] facebook logout only doing logout from website not from the facebook?

查看:163
本文介绍了facebook注销只是从网站注销而不是从Facebook注销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Facebook登录并从网站注销。所以如果用户想要登录网站,他将点击Facebook登录,当他点击注销时,他将自动从Facebook和从网站注销。所以所有这一切,我已经使我的代码这样
在index.html我的代码就像这样

I am doing the facebook login and logout from a website. So if a user wants to login into the site he will click on facebook login and when he will click on logout then he will automatically logout from both facebook and from the site as well. So all for that I have made my code like this In index.html my code is like this

<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
   FB.init({
     appId      : 'XXXXXXXXXXXXX', // App ID
     channelURL : '', // Channel File, not required so leave empty
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     oauth      : true, // enable OAuth 2.0
     xfbml      : false  // parse XFBML
   });
};
// logs the user in the application and facebook
function login(){
FB.getLoginStatus(function(r){
     if(r.status === 'connected'){
            window.location.href = 'fbconnect.php';
     }else{
        FB.login(function(response) {
                if(response.authResponse) {
              //if (response.perms)
                    window.location.href = 'fbconnect.php';
            } else {
              // user is not logged in
            }
     },{scope:'email'}); // which data to access from user profile
 }
});
}

// Load the SDK Asynchronously
(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>
<a href='#' onclick='login();'>Facebook Login</a>

而在fbconnect.php中,我的代码就是这样

and in fbconnect.php my code is like this

<?php
    require 'src/facebook.php';
    $facebook = new Facebook(array(
           'appId'  => 'XXXXXXXXXXXXXXXXX',
           'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
           'cookie' => true,
    ));
    $user = $facebook->getUser();
    $loginUrl = $facebook->getLoginUrl();
    //echo "<a href='$loginUrl'>login</a>";

    $logoutUrl = $facebook->getLogoutUrl();
    //echo $loginUrl; 
    if($user){
        $_SESSION['user_info'] = $user; 
        $_SESSION['user_pro']= $facebook->api('/me');
        echo $_SESSION['user_pro']['first_name'];
        echo $_SESSION['user_pro']['last_name'];
        echo '<pre>';
        //print_r($_SESSION);
        echo '</pre>';
    }
    else{
        echo 'not logged in '; 
    }
    echo "<a href='logout.php'>log out </a>"
?>

在logout.php中我的代码看起来像这样

and in logout.php my code looks like this

<?php
require 'src/facebook.php';    //including the facebook php sdk
$facebook = new Facebook();
$token = $facebook->getAccessToken();
$url = 'index.html';
$facebook->destroySession();
session_destroy();
header('Location: '.$url);
?>

所有这一切都在做网站注销,但没有从Facebook注销。有人可以同时告诉我如何从Facebook和网站注销?任何帮助和建议将会令人欣慰。谢谢

All this is doing the site logout but it is not doing the logout from facebook. So can someone kindly tell me how to do logout from both facebook and website at a same time? Any help and suggestions will be reallly appreciale. Thanks

推荐答案

是的,这不会从Facebook注销。

Yes this will not do logout from Facebook.

我看到您正在使用JS SDK,因此您需要使用JS API进行注销。你可以按照

I see you are using the JS SDK also so you need to use JS API for logout. You can do as

echo '<a href="#" onclick = "fb_logout();" >Logout </a>';

你需要添加以下JS函数,我看到你已经有FB.login

You need to add the following JS function, I see u already have FB.login

<script>
            function fb_logout(){
                    if (FB.getAuthResponse()) {
                        FB.logout(function(){
                            top.location.href = 'logout.php'
                        });
                    }else{  
                        top.location.href = 'logout.php'
                    }
            }
</script>

确保您在fbconnect.php文件中具有以下JS,以便触发FB函数
所以完整的代码是

Make sure you have the following JS in fbconnect.php file so as to trigger the FB functions So the complete code is

<div id="fb-root"></div>

<script type="text/javascript">

  window.fbAsyncInit = function() {
    FB.init({appId: '<?php echo FACEBOOK_APP_ID ; ?>', status: true, cookie: true, oauth: true,
             xfbml: true});
  };
  (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);
  }());

function fb_logout(){
                        if (FB.getAuthResponse()) {
                            FB.logout(function(){
                                top.location.href = 'logout.php'
                            });
                        }else{  
                            top.location.href = 'logout.php'
                        }
                }
</script>

在文件 logout.php 处理您的网站的注销代码。

In the file logout.php you can handle the logout code for your website.

同样用于使用PHP SDK生成注销,您需要执行

Also for generating the logout using the PHP SDK you need to do as

$params = array( 'next' => 'page after logout' );

$logouturl = $facebook->getLogoutUrl($params); // $params is optional.

<a href="<?php echo $logouturl; ?>"> Logout </a>

我建议使用JS SDK进行注销,它可以正常工作,并从Facebook注销。

I would suggest to use JS SDK for logout it works for sure and it logs out from Facebook as well.

这篇关于facebook注销只是从网站注销而不是从Facebook注销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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