我在Firefox中打开了两个标签,一个是我的网站,另一个是Facebook。如果没有授权的应用程序,我会得到一个登录链接,我点击这个,然后提示我验证我这样做的应用程序。
我现在屏幕上显示了getLogoutUrl()提供的注销网址 - 当我点击它时,它会重定向回我的网站,我很满意,但它仍然按照登录方式对我进行分类。如果我刷新了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>';
}
?>
答案 0 :(得分: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>";
}