Facebook PHP SDK – can’t logout (PHP SDK v3.1.1)

I am using the official Facebook PHP SDK for my website. My application lets the users log in and out. Problem is, you can’t really logout although i use the example of facebook. When i click “Logout” on my site the user information is still visible and the logout button is still displayed. Here is the code:


require './src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '111111111111',
  'secret' => '1111111111111111111111',
// Get User ID
$user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    $user = null;

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      h1 a {
        text-decoration: none;
        color: #3b5998;
      h1 a:hover {
        text-decoration: underline;

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <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 ?> 

I found some methods to solve the problem, here is the one:

This is the method used in the example file with SDK

$logoutUrl = $facebook->getLogoutUrl();
<a href="<?php echo $logoutUrl; ?>">Logout</a>

This doesn’t destroy sessions stored in browser.

I modified the logout link as

<a href="?logout=yes">Logout</a>

it means, we change the line

$logoutUrl = $facebook->getLogoutUrl();


$logoutUrl = "?logout=yes";

And i add more code:

	$logoutUrl = $facebook->getLogoutUrl(array('next'=>$redir_url));         

This really works for me.

Leave a Reply