如何退出Facebook

时间:2011-05-13 08:40:17

标签: php facebook facebook-graph-api facebook-php-sdk

我有以下脚本,允许用户使用他们的Facebook帐户登录我的网站,请求相应的权限,如果接受,它将在墙上发布消息:

<?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);
    }
?>

然后我有以下脚本来记录用户:

<?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>

这会清除我创建的会话,但它根本不会将它们从facebook中注销掉。如何清除我创建的会话并将其记录在Facebook外?

4 个答案:

答案 0 :(得分:4)

从您的应用程序和Facebook注销用户的推荐方法是调用javascript SDK的注销功能。

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

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

答案 1 :(得分: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 );

答案 2 :(得分:0)

将此作为您的退出网址:

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

这适用于您的代码:

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

这会将用户退出应用程序,而不是facebook本身,完全退出facebook,这是不推荐的,因为它会让一些用户感到困扰,将其添加到if($session) {

$logoutUrl = $facebook->getLogoutUrl();

然后对于您的退出网址,请使用:

<a href="' . $logoutUrl . '">Logout</a>

答案 3 :(得分:-1)