我正在使用PHP API 将旧的 FB 应用程序迁移到新的图形 API
我有两个页面:公共页面(不需要用户登录)和私人页面(需要)
所以我的应用程序中每个 php 页面的代码如下:
if (this_page_requires_user_login){
$facebook = new Facebook(...) ;
$session = $facebook->getSession ;
if (!$session){
$url =$facebook.getLoginUrl(array(next => current_page );
echo "<fb:redirect url=$url/>" ;
}
// the rest of the code continues here
现在你可以看到,这种工作方式将每个页面转发到登录 url,虽然它可以工作,但它也很慢,并将 &session={bla} 字符串附加到非常单一的 url。
在将用户重定向到登录页面之前,我需要一种方法来检查用户已经登录的位置。但我在 php api 中找不到这样的方法。最好的方法是什么?
编辑
这似乎可以解决问题
if ($session) {
try {
$me = $facebook->api('/me');
if ($me) {
// here comes your code for user who is logged in
}
} catch (FacebookApiException $e) {
login()
}
}else{
login()
}
function login(){
$url =$facebook.getLoginUrl(array(next => current_page );
echo "<fb:redirect url=$url/>" ;
}