我在网站上设置了"使用Facebook登录"选项,我很顺利地实现了这一点。我依赖于facebook->getUser()
方法,根据文档,它是returns the Facebook User ID of the current user, or 0 if there is no logged-in user
。然而,登录后,所有后续对getUser()的调用都会返回userId,即使我手动转到facebook并注销也是如此。似乎正在缓存userId或其他什么。我看到很多人都有同样的问题,但仍然找不到解决方案。我如何克服这个问题(该网站基于CodeIgniter,我指的是本教程)?
从我的网站复制粘贴。
$user = $this->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.
$profile = null;
if($user)
{
try {
// Proceed knowing you have a logged in user who's authenticated.
$profile = $this->facebook->api('/me?fields=id,name,link,email');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
看看如果API没有响应,$user是如何变为null的?这是关键。如果你不走那一步,它就不会起作用。getUser有时会返回一个ID,即使用户最近注销了。