【发布时间】: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