[英]Using Facebook PHP-SDK, how can I determine if a user has logged out of Facebook?
我正在創建一個與Facebook集成的簡單概念驗證網絡應用。 基本功能是它需要顯示有關用戶的基本信息,以及一些他們的朋友和朋友個人資料圖片。 一切似乎都有效,減去了注銷-當用戶單擊通過Facebook PHP-SDK生成的我的Web應用程序上的注銷鏈接時,頁面似乎只是刷新了-用戶顯然仍登錄到我的Web應用程序。 但是,他們已從Facebook正確注銷。 知道為什么我會看到這種行為嗎? 我的代碼如下:
<?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
函數,因為它將用戶數據保存在會話中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.