如何检查用户是否用新的facebook php api登录 [英] How to check if user is logged in with new facebook php api

查看:221
本文介绍了如何检查用户是否用新的facebook php api登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP API 将旧的FB应用迁移到新的图形API



我有两个页面:公开的(不需要用户登录)和私有的页面(这样做)



  if(this_page_requires_user_login){
$ Facebook =新Facebook(...);
$ session = $ facebook-> getSession;
if(!$ session){
$ url = $ facebook.getLoginUrl(array(next => current_page);
echo< fb:redirect url = $ url /> ;

}
//其余的代码继续在这里

现在,您可以看到,这种将每个页面转发到登录URL的方式,同时它也很慢,并将& session = {bla}字符串附加到非常单一的URL。



我需要一种方法来检查用户是否已经登录,然后我将其重定向到loginpage,但是我无法在php api中找到这样的方法。 ?



编辑



这似乎是窍门

  if($ session){
try {
$ me = $ facebook-> api('/ me' ;
如果($ me){
//这里是您登录的用户的代码
}
} catch(FacebookApiException $ e){
login )
}
} else {
logi n()
}

函数login(){

$ url = $ facebook.getLoginUrl(array(next =>当前页面 );


echo< fb:redirect url = $ url /> ;
}


解决方案

如果我正在阅读你的代码正确的,只有没有会话返回你做重定向?根据 Facebook的例子的评论,即使你会回来一个会话,你不能认为它仍然有效。只有尝试一个需要登录用户的API调用,您才能确定。这是我看到可靠地确定登录/注销状态的最佳方式。

  if($ session){
尝试{
$ me = $ facebook-> api('/ me');
if($ me){
//用户登录
}
} catch(FacebookApiException $ e){
//用户未登录
}
}


I am migrating my old FB app to the new graph API using the PHP API

I have two pages: public ones (which require no user login) and private ones (which do)

So the code of every single php page in my application works as follows:

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

Now as you can see, this way of working forwards every single page to the login url and while it works, it is also slow and appends the &session={bla} string to very single url.

I need a way to check where a user is already logged in before I redirect him to loginpage. But i can not find such method in the php api. What's the best way to do this?

EDIT

This seemed to do the trick

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/>" ;
}

解决方案

If I am reading your code correctly, only if no session is returned do you do the redirect? According to comments in Facebook's example, even if you get a session back, you can't assume it's still valid. Only trying an API call that requires a logged in user will you know for sure. This is the best way I've seen to reliably determine login/logout status.

if ($session) {
  try {
    $me = $facebook->api('/me');
    if ($me) {
      //User is logged in
    }
  } catch (FacebookApiException $e) {
    //User is not logged in
  }
}

这篇关于如何检查用户是否用新的facebook php api登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆