Facebook PHP Destroy Session -
despite me trying hours on this, can't figure out why can't destroy facebook session. logout url logs out of facebook, can't destroy session. i've trawled through stack overflow none of answers have helped far.
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 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 saved access_token $session = new facebooksession( $_session['fb_token'] ); // validate access_token make sure it's still valid try { if ( ! $session->validate() ) { $session = null; } } catch ( exception $e ) { // catch exceptions $session = null; } } else { // no session exists try { $session = $helper->getsessionfromredirect(); } catch( facebookrequestexception $ex ) { // when facebook returns error } catch( exception $ex ) { // when validation fails or other local issues echo $ex->message; } } // check if session exists if ( isset( $session ) ) { // save session $_session['fb_token'] = $session->gettoken(); // create session using saved token or new 1 generated @ login $session = new facebooksession( $session->gettoken() ); // create logout url $logouturl = $helper->getlogouturl( $session, 'http://msbooth.azurewebsites.net/' );
here problem section:
if(isset($_get['action']) && $_get['action'] === 'logout'){ $session->destroysession(); header('location: ' . $helper->getlogouturl($session, 'xxx')); }
any ideas on why session won't destroy welcome! don't think i'm calling action on right object. rest of code below reference.
} else { // no session - login url $loginurl = $helper->getloginurl( $permissions ); echo '<a href="' . $loginurl . '">log in facebook</a>'; }
funnily enough seems have fixed issue. best practice or fail?
// create logout url (logout page should destroy 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); }
Comments
Post a Comment