title: Facebook PHP Destroy Session
body: <p>Despite me trying for hours on this, I can't figure out why I can't destroy this Facebook session. The logout URL logs out of Facebook, but i can't destroy the session. I've trawled through Stack Overflow but none of the answers have helped so far.</p>
  
  <pre><code>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 ) &amp;&amp; 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-&gt;validate() ) {
          $session = null;
        }
      } catch ( Exception $e ) {
        // Catch any exceptions
        $session = null;
      }
  } else {
    // No session exists
    try {
      $session = $helper-&gt;getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
  
      // When Facebook returns an error
    } catch( Exception $ex ) {
  
      // When validation fails or other local issues
      echo $ex-&gt;message;
    }
  }
  // Check if a session exists
  if ( isset( $session ) ) {
    // Save the session
    $_SESSION['fb_token'] = $session-&gt;getToken();
    // Create session using saved token or the new one we generated at login
    $session = new FacebookSession( $session-&gt;getToken() );
  
    // Create the logout URL
    $logoutURL = $helper-&gt;getLogoutUrl( $session, 'http://msbooth.azurewebsites.net/' );
  </code></pre>
  
  <p>HERE IS THE PROBLEM SECTION:</p>
  
  <pre><code>  if(isset($_GET['action']) &amp;&amp; $_GET['action'] === 'logout'){
          $session-&gt;destroySession();
          header('Location: ' . $helper-&gt;getLogoutUrl($session, 'XXX'));
      }
  </code></pre>
  
  <p>Any ideas on why the session won't destroy are welcome! I don't think i'm calling the action on the right object. Rest of the code below for reference.</p>
  
  <pre><code>} else {
    // No session - Get Login URL
    $loginUrl = $helper-&gt;getLoginUrl( $permissions );
  
    echo '&lt;a href="' . $loginUrl . '"&gt;Log in with Facebook&lt;/a&gt;';
  }
  </code></pre>