【问题标题】:Facebook PHP Destroy SessionFacebook PHP 销毁会话
【发布时间】:2015-08-24 23:40:52
【问题描述】:

尽管我为此尝试了几个小时,但我无法弄清楚为什么我不能破坏这个 Facebook 会话。注销 URL 从 Facebook 注销,但我无法销毁会话。我已经浏览了 Stack Overflow,但到目前为止没有任何答案有帮助。

session_start();
$app_id = 'XXX';
$app_secret = 'XXX';
$redirect_uri = 'XXX';
$permissions = array(XXX);
define( 'ROOT', dirname( __FILE__ ) . '/' );
require_once( ROOT . 'facebook-php-sdk-v4-4.0-dev/autoload.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

// Initialize the SDK
FacebookSession::setDefaultApplication( $app_id, $app_secret );
$helper = new FacebookRedirectLoginHelper( $redirect_uri );

// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
  // Create new session from saved access_token
  $session = new FacebookSession( $_SESSION['fb_token'] );
    // Validate the access_token to make sure it's still valid
    try {
      if ( ! $session->validate() ) {
        $session = null;
      }
    } catch ( Exception $e ) {
      // Catch any exceptions
      $session = null;
    }
} else {
  // No session exists
  try {
    $session = $helper->getSessionFromRedirect();
  } catch( FacebookRequestException $ex ) {

    // When Facebook returns an error
  } catch( Exception $ex ) {

    // When validation fails or other local issues
    echo $ex->message;
  }
}
// Check if a session exists
if ( isset( $session ) ) {
  // Save the session
  $_SESSION['fb_token'] = $session->getToken();
  // Create session using saved token or the new one we generated at login
  $session = new FacebookSession( $session->getToken() );

  // Create the logout URL
  $logoutURL = $helper->getLogoutUrl( $session, 'http://msbooth.azurewebsites.net/' );

这里是问题部分:

  if(isset($_GET['action']) && $_GET['action'] === 'logout'){
        $session->destroySession();
        header('Location: ' . $helper->getLogoutUrl($session, 'XXX'));
    }

欢迎任何关于为什么会话不会破坏的想法!我不认为我在调用正确的对象上的动作。下面的其余代码供参考。

} else {
  // No session - Get Login URL
  $loginUrl = $helper->getLoginUrl( $permissions );

  echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
}

【问题讨论】:

    标签: php facebook session destroy


    【解决方案1】:

    有趣的是,这似乎已经解决了这个问题。这是最佳做法还是会失败?

          // Create the logout URL (logout page should destroy the session)
      $logoutURL = $helper->getLogoutUrl( $session, 'http://msbooth.azurewebsites.net/' );
      $redirectlogout = 'Location: ' . $helper->getLogoutUrl($session, 'http://msbooth.azurewebsites.net/');
      if(isset($_GET['action']) && $_GET['action'] === 'logout'){
            session_destroy();
            header($redirectlogout);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-05-25
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2015-03-20
      • 1970-01-01
      相关资源
      最近更新 更多