0

I have the following script which will allow a user to login to my website using their facebook account, request the appropriate permissions, and if accepted, it will post a message on a wall:

<?php
    require 'facebook.php';

    $facebook = new Facebook(array(
        'appId'  => 'removed for security reasons',
    'secret' => 'removed for security reasons',
        'cookie' => true,
    ));

    $session = $facebook->getSession();

    if ($session) {

        if (isset($_GET[id])) {
            $post = $facebook->api("/" . $_GET['id'] . "/feed", "POST",  array('message' => 'Hello!'));
            echo 'A message has been posted on your friends wall';

        } else {

            $friends = $facebook->api('/me/friends');

            foreach ($friends as $key=>$value) {
                echo 'You have ' . count($value) . ' friends<br />';

                foreach ($value as $fkey=>$fvalue) {
                    echo 'friend id = ' . $fvalue[id] . ' - friend name = ' . $fvalue[name] . ' - <a href="/stage0.php?id=' . $fvalue[id] . '">post message</a><br />';
                }
            }
        }

    } else {

        $loginUrl = $facebook->getLoginUrl(array(
            'req_perms' => 'publish_stream',
            'next' => 'http://'.$_SERVER['SERVER_NAME'].'/stage0.php',
            'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.php',
        ));

        header('Location: '.$loginUrl);
    }
?>

I then have the following script to log the user out:

<?php
    error_reporting( E_ALL | E_STRICT );
    ini_set('display_errors', 1);
?>

<?php
    session_start();
?>

<html>
    <head>
        <title></title>

        <?php
            $_SESSION = array(); 
            session_destroy();
        ?>

        <meta http-equiv="refresh" content="0;index.php">
    </head>

    <body>
        <h1>logout</h1>
    </body>
</html>

This clears the sessions I created, but it does not log them out of facebook at all. How do I clear the sessions I create and also log them out of facebook?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

4 Answers4

4

Recommended way to log out a user from both your application and Facebook, is to call the logout feature of the javascript SDK.

<a href="/auth/logout" onclick="FB.logout();">logout</a>

check : http://developers.facebook.com/docs/reference/javascript/FB.logout/

Jean-Christophe Meillaud
  • 1,961
  • 1
  • 21
  • 27
  • How do I do this at the serverside. I am login in via the serverside, so logging out from the serverside would be ideal for my application. – oshirowanen May 13 '11 at 08:56
  • You had to do both, that's why I let the href /auth/logout, which is on my serverside, then i can do whatever i want, when i received a call there ;) THe onclick javascript function is mainly for client side – Jean-Christophe Meillaud May 13 '11 at 11:17
  • could you please provide more info about /auth/logout as I do not want to do this client side, I want to do this server side. – oshirowanen May 13 '11 at 11:31
  • I'm just deleting the session, serverside, as i'm mainly using zend, i do something like : Zend_Registry::get('auth')->clearIdentity() ... – Jean-Christophe Meillaud May 15 '11 at 22:52
1
/* Use access token i.e. $_SESSION['facebookAccessToken']*/

function logoutUser($sessionKey)
{
    include_once 'facebook.php';
    $facebook = new Facebook( array('appId'=>FACEBOOK_APP_ID , 'secret'=>FACEBOOK_APP_SEC , 'cookie' => true));
    $logoutUrl = $facebook->getLogoutUrl(array('next' => BASE_URL , 'session_key' => $sessionKey));
    return $logoutUrl;
}

$access_array = explode('|' , $_SESSION['facebookAccessToken']);

$sessionKey = $access_array[1];

$redirectUrl = logoutUser($sessionKey);

header('Location: '.$redirectUrl );
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
0

Use this as your logout URL:

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

And this for your code:

if(isset($_GET['action']) && $_GET['action'] === 'logout'){
    $facebook->destroySession();
}

This will log the user out of the app, rather than facebook itself, logging out of facebook altogether, which is not recommended, as it will distress some users, add this to your session where if($session) { is

$logoutUrl = $facebook->getLogoutUrl();

then for your logout URL, use:

<a href="' . $logoutUrl . '">Logout</a>
Blake
  • 63
  • 1
  • 1
  • 11
  • 1
    It's actually required in the platform policies to log the user out of Facebook. Not saying it makes sense, but it's there. #6 @ https://developers.facebook.com/policy/ – Frans Aug 05 '13 at 15:32
  • Ahh, wow, thanks for pointing that out! Like you said, it doesn't make sense, if I was to log out of an app/website when using facebook, i'd like to stay logged into facebook, wonder if you could have to options to log out of the app and facebook, will take a look into that, but thanks for pointing it out. – Blake Aug 05 '13 at 18:06
-1

Facebook Oauth Logout

Community
  • 1
  • 1
Janis Veinbergs
  • 6,907
  • 5
  • 48
  • 78