简体   繁体   中英

Using Facebook PHP-SDK, how can I determine if a user has logged out of Facebook?

I'm creating a simple proof-of-concept web-app that integrates with Facebook. The base functionality is that it needs to display basic info about the user, and a few of their friends + friend profile pictures. All of it seems to work, minus logging out - when the user clicks the logout link on my web app, which is generated via the Facebook PHP-SDK, the page simply appears to refresh - the user is clearly still logged into my web app. They are, however, logged out of Facebook proper. Any idea why I'm seeing this behavior? My code is as follows:

<?php

require 'sdk/facebook.php';

$facebook = new Facebook(
    array(
        'appId' => 'xxx' //actual app ID and secret go here,
        'secret' => 'xxx'
    )
);

##
## Instantiate user object if a logged in user exists
if ($user = $facebook->getUser()) {
    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        $user = NULL;
    }
}

##
## Set login/logout url
$url = $user ? $facebook->getLogoutUrl() : $facebook->getLoginUrl();

##
## Instantiate friend object data
if ($user) {
    try {
        $friend_api_data = $facebook->api('/me/friends');
        $friends = $friend_api_data['data'];
        // Shuffle friends
        shuffle($friends);
        $shuffled_friends = array();
        for ($i = 0; $i < count($friends) && $i < 6; $i++) {
            $shuffled_friends[] = $friends[$i];
        }   
    } catch (FacebookApiException $e) {

    }

}

?>
<!doctype html>
<html>
<head>
    <title>Test</title>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <h1>Facebook Integration</h1>

        <!-- Login/Logout URL -->
        <a href="<?= $url ?>"><?= $user ? 'Logout' : 'Login' ?></a>

        <!-- Current User Data -->
        <?php if ($user): ?>
            <h2>Hi, <?= $user_profile['name'] ?></h2>
            <img src="https://graph.facebook.com/<?= $user ?>/picture" />

            <pre>
                <?= print_r($user_profile, TRUE) ?>
            </pre>
        <?php endif; ?>

        <!-- Friend Data -->
        <?php if($shuffled_friends): ?>
            <h2>Your Friends</h2>
            <ul>
                <?php foreach($shuffled_friends as $f): ?>
                    <li>
                        <a href="http://www.facebook.com/<?= $f['id'] ?>">
                            <img src="https://graph.facebook.com/<?= $f['id'] ?>/picture" />
                            <?= $f['name'] ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>  
</body>
</html>

如果您使用的是PHP SDK的第3版,则还需要调用destroySession函数,因为它将用户数据保存在会话中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM