0

我在 Firefox 中打开了 2 个选项卡,一个是我的网站,另一个是 Facebook。在没有授权应用程序的情况下,我会获得一个登录链接,然后单击此链接,然后系统会提示我对我这样做的应用程序进行身份验证。

我现在在屏幕上显示了 getLogoutUrl() 提供的注销 URL - 当我单击它时,它会重定向回我很满意的网站,但它仍然将我归类为已登录。如果我在 Facebook 上刷新另一个选项卡我现在退出了。会议上似乎有些宿醉?

<?php
require_once("fb/facebook.php");

$config = array();
$config['appId'] = '...';
$config['secret'] = '...';
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config);
$user_id = $facebook->getUser();

if($user_id) {
    //Got an ID - Facebook says we're logged in
    try {

        $user_profile = $facebook->api('/me','GET');
        echo "<img src=\"http://graph.facebook.com/".$user_profile['username']."/picture\"><a href=\"".$user_profile['link']."\">" . $user_profile['name']."</a>";
        $logout_url = $facebook->getLogoutUrl();
        echo "<a href=\"" . $logout_url . "\">Logout</a><br><br><br>";

    } 
    catch(FacebookApiException $e) {
        $login_url = $facebook->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
    }   
} 
else {
    // Not logged in - give URL to login
    $login_url = $facebook->getLoginUrl();
    echo 'Please <a href="' . $login_url . '">login.</a>';
}
?>
4

1 回答 1

0

您需要销毁会话。

$config = array();
$config['appId'] = 'xxxx';
$config['secret'] = 'xxxxx';

$facebook = new Facebook($config);

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


$user = $facebook->getUser();
echo $user;
if ($user) {

    try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
    }
    echo "<pre>";
    print_r($user_profile);
    echo "</pre>";  
    $logout = $facebook->getLogoutUrl();

    //echo $logout;

    echo "<a href='test.php?act=logout'>Logout</a>";


} else {

    $login = $facebook->getLoginUrl(array("scope"=>"email","display"=>"popup","redirect_uri"=>"http://domain.com/test.php"));

    echo "<a href='".$login."'>Login</a>";

}
于 2013-10-08T15:11:34.367 回答