Facebook PHP SDK - 不会正确注销

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10366366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:00:20  来源:igfitidea点击:

Facebook PHP SDK - will not logout properly

phpfacebookoauthfacebook-php-sdk

提问by garethdn

I've been searching for hours for the solution to this problem but can't find one that works for me. When i click "Logout" on my site the user information is still visible and the logout button is still displayed. Here is the code:

我一直在寻找解决此问题的方法数小时,但找不到适合我的方法。当我在我的网站上单击“注销”时,用户信息仍然可见,并且注销按钮仍然显示。这是代码:

require 'facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'xxxx',
  'secret' => 'xxxx',
));

// Get User ID
$user = $facebook->getUser();
var_dump($user);
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.

if ($_GET['logout'] == "yes") {
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', 'http://gno.....ment/index.php');
session_destroy();
header("Location: ".$_SERVER['PHP_SELF']."");
}

if ($user_profile) {
  $logoutUrl = $facebook->getLogoutUrl;
} else {
  $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_status',
  'canvas' => 1,
  'fbconnect' => 0,
  'redirect_uri' => 'http://gno.....ment/index.php'));
}

..... .....

………………

<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout of FB</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>

It seems that if ($_GET['logout'] == "yes")might be the answer for me but i can't get it working. I don't know where logoutis gotten from or where it is defined?

似乎这if ($_GET['logout'] == "yes")可能是我的答案,但我无法让它工作。我不知道logout从哪里得到或从哪里定义?

This seems to be a common issue but i can't figure it out. I'd really appreciate some help.

这似乎是一个常见问题,但我无法弄清楚。我真的很感激一些帮助。

回答by Tosh

Doing it with PHP SDK is really easy, the documentation is just really awfull. You do not need to redirect to Facebook. You just have to clear the session that the Facebook class sets, there is a function for that in the Facebook base class called destroySession(). Here I'm doing it on a get.

用 PHP SDK 做这件事真的很容易,文档真的很糟糕。您不需要重定向到 Facebook。您只需要清除 Facebook 类设置的会话,在 Facebook 基类中有一个名为destroySession()的函数。在这里,我正在做。

require_once('libs/facebook.php');

$facebook = new Facebook(array(
    'appId'  => '1121111110112',
    'secret' => 'bcfsaasaaaaaa2b7adsae3a4dd5'
)); 

if(isset($_GET['action']) && $_GET['action'] === 'logout'){
    $facebook->destroySession();
}

The $facebook->getLogoutUrl() logs the user out of Facebook.

$facebook->getLogoutUrl() 将用户从 Facebook 注销。

回答by Hüseyin BABAL

You can solve this problem by specifying external logout problem. You can have a look at here

您可以通过指定外部注销问题来解决此问题。你可以看看这里

for detail information. It is a good tutorial for this problem.

详细信息。这是一个很好的教程来解决这个问题。

Hope this helps

希望这可以帮助

回答by slava

To answer directly to your question

直接回答您的问题

... I don't know where logout is gotten from or where it is defined?

...我不知道注销是从哪里获得的或在哪里定义的?

When you create your logout url, add additional parameter 'logout'

创建注销 url 时,添加附加参数“注销”

$logoutUrl = $facebook->getLogoutUrl(array(
    'next'=>'http://yourdomain.com/facebook-test-search.php?logout=yes'
));

Then in your script, you clear session and cookies when isset($_GET['logout'])

然后在您的脚本中,您清除会话和 cookie 时 isset($_GET['logout'])

回答by Charles Blackwell

Here is how I logout using the latest PHP-SDK:

以下是我如何使用最新的 PHP-SDK 注销:

login.php

登录.php

require_once("php-sdk/facebook.php");

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxx',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logout_params = array('next'=>'http://www.pittsburghpartycentral.com/logout.php');
  $logoutUrl = $facebook->getLogoutUrl($logout_params);
} else {
    $login_params = array(
                        'scope' => 'email',
                        'display' => 'popup'
                        );
  $loginUrl = $facebook->getLoginUrl($login_params);
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout (<?php echo $user_profile[first_name]; ?>)</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>" onclick="javascript:void window.open('<?php echo $loginUrl; ?>','fb_popup','width=600,height=300,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=0,left=0,top=0');return false;">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

logout.php

登出.php

<?php 
    session_start();            //start session
    $_SESSION = array();    //clear session array
    session_destroy();      //destroy session
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Log Out</title>
</head>

<body>
<p>You have successfully logged out!</p>
<p>Return to the <a href="connect.php">connect</a> page</p>

</body>
</html>

回答by nelsonec87

I was able do logout the user from my app using:

我可以使用以下方法从我的应用程序中注销用户:

$facebook->destroySession();

The

$facebook->getLogoutUrl();

makes the user be logged out from facebook, not from your app.

使用户从 Facebook 注销,而不是从您的应用注销。

回答by Дмитро Булах

Had some kind of similar trouble with that. Even

有一些类似的麻烦。甚至

$facebook->destroySession();

didn't work properly until I removed

在我删除之前无法正常工作

$facebook->getLogoutUrl();

call completely. getLogOutUrl()added some parameter that conflicted later with my .htaccess and led to *"mod_fcgid: stderr: CSRF state token does not match one provided"* error.

完全调用。getLogOutUrl()添加了一些稍后与我的 .htaccess 冲突并导致 *"mod_fcgid: stderr: CSRF state token does not match one provided"* 错误的参数。

回答by Alexander Farber

Because I still have PHP 5.3 at my CentOS 6.7 server in year 2016 and don't want to take the hassle of upgrading the PHP version - I still use the old facebookarchive/facebook-php-sdkinstead of the newer facebook/facebook-php-sdk-v4library.

因为我在 2016 年的 CentOS 6.7 服务器上仍然有 PHP 5.3 并且不想麻烦升级 PHP 版本 - 我仍然使用旧的facebookarchive/facebook-php-sdk而不是较新的facebook/facebook-php -sdk-v4库。

And here is how I handle the logout in my app:

这是我在我的应用程序中处理注销的方式:

<?php

require_once('facebook-php-sdk-3.2.3/src/facebook.php');

const TITLE      = 'My amazing app';
const REDIRECT   = 'https://example.com/myapp/';

#Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
#Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

$client = new Facebook(array(
        'appId'  => REPLACE_ME,
        'secret' => REPLACE_ME,
));

if (isset($_REQUEST['logout'])) {
        $client->destroySession();
        header('Location: ' . REDIRECT);
        exit(0);
}

if ($client->getUser()) {
        try {
                $me = $client->api('/me?fields=id,first_name,gender');
                $body = '<PRE>' . print_r($me, TRUE) . '</PRE>';
        } catch (FacebookApiException $ex) {
                error_log($ex);
                $body = '<PRE>' . htmlspecialchars($e->getMessage()) . '</PRE>';
        }
} else {
        $body = sprintf('<P><A HREF="%s">Login</A></P>', $client->getLoginUrl());
}

?>

<!DOCTYPE HTML>
<HTML>
<HEAD>
        <TITLE><?= TITLE ?></TITLE>
</HEAD>
<BODY>
        <?= $body ?>
        <P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P>
</BODY>
</HTML>

Do not forget to -

不要忘记 -

  1. Get web client id and secret at Facebook console
  2. Authorize the https://example.com/myapp/at the same place
  1. Facebook 控制台获取 Web 客户端 ID 和密码
  2. https://example.com/myapp/在同一个地方授权

回答by wroniasty

I remember this was a huge pain in one of my apps. It seems that finally what seemed to work was:

我记得这对我的一个应用程序来说是一个巨大的痛苦。似乎最后似乎有效的是:

jQuery(function() {
   /* ... */
   FB.logout();
   window.location = 'some url';
});

I should be about the same without jQuery (just run FB.logout() at page load). AFAIR I just could not get this to work on the server-side in PHP. Hope it helps :).

如果没有 jQuery,我应该也差不多(只需在页面加载时运行 FB.logout())。AFAIR 我只是无法让它在 PHP 的服务器端工作。希望能帮助到你 :)。