【问题标题】:Facebook PHP SDK - will not logout properlyFacebook PHP SDK - 无法正确注销
【发布时间】:2012-04-28 18:29:11
【问题描述】:

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

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

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

这似乎是一个常见问题,但我无法弄清楚。非常感谢您的帮助。

【问题讨论】:

    标签: php facebook oauth facebook-php-sdk


    【解决方案1】:

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

    require_once('libs/facebook.php');
    
    $facebook = new Facebook(array(
        'appId'  => 'xxxxxx',
        'secret' => 'xxxxxx'
    )); 
    
    $facebook->destroySession();
    

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

    【讨论】:

    • 谢谢!在你指出之前,我不知道它让我退出 Facebook。
    【解决方案2】:

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

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      直接回答您的问题

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

      当你创建你的注销 url 时,添加额外的参数 'logout'

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

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

      【讨论】:

        【解决方案4】:

        这是我使用最新的 PHP-SDK 注销的方法:

        登录.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>
        

        注销.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>
        

        【讨论】:

          【解决方案5】:

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

          $facebook->destroySession();
          

          $facebook->getLogoutUrl();
          

          使用户从 facebook 中退出,而不是从您的应用中退出。

          【讨论】:

            【解决方案6】:

            遇到了类似的麻烦。 甚至

            $facebook->destroySession();
            

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

            $facebook->getLogoutUrl();
            

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

            【讨论】:

              【解决方案7】:

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

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

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

              别忘了-

              1. Facebook console 获取 Web 客户端 ID 和密码
              2. 在同一地点授权https://example.com/myapp/

              【讨论】:

                【解决方案8】:

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

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

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

                【讨论】:

                • 那么你是建议我使用 jQuery 还是只使用 FB.logout()?我应该把这个放在我的代码中的什么地方?
                • 您应该在客户端的任何onready 处理程序中运行FB.logout。 (jQuery 正是我用来设置onready 处理程序的工具)。
                • 它不应该清除 PHP 会话,它会杀死 Facebook 端的 Facebook 会话。
                猜你喜欢
                • 1970-01-01
                • 2012-03-06
                • 2011-05-31
                • 2013-07-29
                • 1970-01-01
                • 2015-08-01
                • 1970-01-01
                • 2011-09-17
                • 1970-01-01
                相关资源
                最近更新 更多