在我开始实现Facebook身份验证之前,我的注销控制器看起来是这样的:
# Kill the session
$this->session->sess_destroy();
# Redirect back to main page.
redirect('', 'location');
我试着修改它,让用户也退出Facebook,但没有成功。
# Kill the session
$this->session->sess_destroy();
require 'application/sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '625256',
'secret' => '25256256',
'cookie' => true,
));
$session = $facebook->getSession();
if ($session) {
$location = (string) html_entity_decode($facebook->getLogoutUrl(array('next' => 'http://localhost:8888/taketocollege/')));
header("Location: $location");
exit();
}
# Redirect back to main page.
redirect('', 'location');
我做错了什么?我不想为此使用Javascript SDK,因为我希望所有这些都在控制器中完成。
谢谢!!
编辑:更清楚地说,问题是会话没有被破坏。我的注销控制器将用户发送到主页,但主页会将带有会话的Facebook用户重定向到登录页面。因此,Facebook用户最终出现在登录页面上。通常情况下,登录页面会自动让他们登录,但到那时,他们的会话就会被破坏。
这里有一个指向注销控制器的"注销"按钮,您正试图让注销控制器调用一些东西来让用户从Facebook注销。
如果你用Facebook登录你的用户,你不需要注销控制器。您只需在页面中显示注销URL:
$args['next'] = "http://localhost:8888/taketocollege/";
$logoutUrl = $facebook->getLogoutUrl($args);
echo '<a href="' . $logoutUrl . '">Login with Facebook</a>';
如果你想在用户注销时清除会话,你可以在next
页面上(对你来说,http://localhost:8888/taketocollege/
):
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
if (!$user) {
$this->session->sess_destroy();
}
希望能有所帮助!
我的代码最初将用户重定向到主页。
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
if($session && !$this->session->userdata('logged_in'))
redirect('login');
该代码将用户重定向到登录页面,因为会话已经更改,而不是完全删除。
通过更改最后两行,我能够解决这个问题:
if($me && !$this->session->userdata('logged_in'))
redirect('login');
希望它能帮助到别人!