我一直在寻找解决这个问题的时间,但是找不到适合我的问题。当我点击我网站上的“注销”时,用户信息仍然可见,并且仍然显示注销按钮。这是代码:
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
// Get User ID
$user = $facebook->getUser();
var_dump($user);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($_GET['logout'] == "yes") {
setcookie('fbs_'.$facebook->getAppId(), '', time()-100, '/', 'http://gno.....ment/index.php');
session_destroy();
header("Location: ".$_SERVER['PHP_SELF']."");
}
if ($user_profile) {
$logoutUrl = $facebook->getLogoutUrl;
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream,user_status',
'canvas' => 1,
'fbconnect' => 0,
'redirect_uri' => 'http://gno.....ment/index.php'));
}
..... .....
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout of FB</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
似乎if ($_GET['logout'] == "yes")
可能是我的答案,但我无法让它发挥作用。我不知道logout
从哪里获得或在哪里定义?
这似乎是一个常见问题,但我无法弄明白。我真的很感激一些帮助。
答案 0 :(得分:20)
使用PHP SDK进行操作非常简单,文档非常简单。您无需重定向到Facebook。您只需要清除Facebook类设置的会话,在Facebook基类中有一个名为 destroySession()的函数。在这里,我正在做这件事。
require_once('libs/facebook.php');
$facebook = new Facebook(array(
'appId' => '1121111110112',
'secret' => 'bcfsaasaaaaaa2b7adsae3a4dd5'
));
if(isset($_GET['action']) && $_GET['action'] === 'logout'){
$facebook->destroySession();
}
$ facebook-&gt; getLogoutUrl()将用户从Facebook中删除。
答案 1 :(得分:3)
答案 2 :(得分:2)
直接回答您的问题
... 我不知道退出的位置或定义的位置?
创建注销网址时,请添加其他参数“注销”
$logoutUrl = $facebook->getLogoutUrl(array(
'next'=>'http://yourdomain.com/facebook-test-search.php?logout=yes'
));
然后在您的脚本中,在isset($_GET['logout'])
答案 3 :(得分:2)
以下是我使用最新PHP-SDK注销的方法:
的login.php
require_once("php-sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logout_params = array('next'=>'http://www.pittsburghpartycentral.com/logout.php');
$logoutUrl = $facebook->getLogoutUrl($logout_params);
} else {
$login_params = array(
'scope' => 'email',
'display' => 'popup'
);
$loginUrl = $facebook->getLoginUrl($login_params);
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout (<?php echo $user_profile[first_name]; ?>)</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>" onclick="javascript:void window.open('<?php echo $loginUrl; ?>','fb_popup','width=600,height=300,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=0,left=0,top=0');return false;">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
logout.php
<?php
session_start(); //start session
$_SESSION = array(); //clear session array
session_destroy(); //destroy session
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Log Out</title>
</head>
<body>
<p>You have successfully logged out!</p>
<p>Return to the <a href="connect.php">connect</a> page</p>
</body>
</html>
答案 4 :(得分:1)
我可以使用以下方式从我的应用中注销用户:
$facebook->destroySession();
$facebook->getLogoutUrl();
让用户从Facebook注销,而不是从您的应用中注销。
答案 5 :(得分:0)
有一些类似的麻烦。 甚至
$facebook->destroySession();
在我删除
之前无法正常工作$facebook->getLogoutUrl();
完全打电话。 getLogOutUrl()
添加了一些与我的.htaccess冲突的参数,并导致*“mod_fcgid:stderr:CSRF状态令牌与提供的”*错误不匹配。
答案 6 :(得分:0)
因为我在2016年的CentOS 6.7服务器上仍然使用PHP 5.3并且不想轻松升级PHP版本 - 我仍然使用旧的facebookarchive/facebook-php-sdk而不是较新的facebook/facebook-php-sdk-v4库。
以下是我在应用程序中处理注销的方法:
<?php
require_once('facebook-php-sdk-3.2.3/src/facebook.php');
const TITLE = 'My amazing app';
const REDIRECT = 'https://example.com/myapp/';
#Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
#Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$client = new Facebook(array(
'appId' => REPLACE_ME,
'secret' => REPLACE_ME,
));
if (isset($_REQUEST['logout'])) {
$client->destroySession();
header('Location: ' . REDIRECT);
exit(0);
}
if ($client->getUser()) {
try {
$me = $client->api('/me?fields=id,first_name,gender');
$body = '<PRE>' . print_r($me, TRUE) . '</PRE>';
} catch (FacebookApiException $ex) {
error_log($ex);
$body = '<PRE>' . htmlspecialchars($e->getMessage()) . '</PRE>';
}
} else {
$body = sprintf('<P><A HREF="%s">Login</A></P>', $client->getLoginUrl());
}
?>
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE><?= TITLE ?></TITLE>
</HEAD>
<BODY>
<?= $body ?>
<P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P>
</BODY>
</HTML>
别忘了 -
https://example.com/myapp/
答案 7 :(得分:-1)
我记得在我的一个应用程序中这是一个巨大的痛苦。似乎最终似乎有效的是:
jQuery(function() {
/* ... */
FB.logout();
window.location = 'some url';
});
如果没有jQuery,我应该是一样的(只需在页面加载时运行FB.logout())。 AFAIR我无法在PHP的服务器端使用它。希望它有所帮助:)。