facebook登录手机$ fb_user = $ facebook-> getUser()在首次加载时不起作用,但在浏览器上运行正常

时间:2012-09-11 09:03:57

标签: facebook mobile login

移动用户尝试登录我的网站时出现问题,而登录在桌面浏览器上运行正常。我已将facebook apps dev部分中的Mobile URL设置为与浏览器相同。

在第一次加载时,它无法获得'$ fb_user = $ facebook-> getUser();' (它返回0)然后重新加载,因为它没有在第一次登录,然后我得到fb信息('CSRF状态标记与提供的不匹配'。)。然后我有另一个重新加载,它没有再次获取fb_user,因此然后陷入此登录/无登录循环。使用普通浏览器,它第一次获取$ fb_user,然后后续代码与重定向一起使用。

fb代码看起来像这样:

function fb_fresh() {

  global $user, $loginUrl;

  //Add the FB library code. This is version 3.1.1. have updated this to the latest version as of 10 Sept 2012
  $path_fb = "sites/all/modules/custom/fb/facebook.php";
  require_once $path_fb;  //make sure curl is installed to work

  // Create our Application instance. (not revealed for obvious reasons)
    $facebook = new Facebook(array('appId' => '###','secret' => '###','cookie' => true,));

  // Get User array
  $fb_user = $facebook->getUser();
  //printing error logs to see where things go wrong.. 
  error_log("function fb(). fbuser = " . $fb_user . ". Drupal user->uid = " . $user->uid, 0);


  // $fb_user = 0 when no logged-in user. https://developers.facebook.com/docs/reference/php/facebook-getUser/
  if ($fb_user != "0") {
    $logoutUrl = $facebook->getLogoutUrl();
    $sessionURL = "<a href='" . $logoutUrl . "' target='_top'>Logout</a>";
  } else {

      //Get the permissions + once the user has accepted permissions send them to a page that redirects them back to the Facebook app, which then automatically goes to the mobile version if necessary
      $params_login = array(
          'scope' => 'email, user_location, friends_location, user_status, friends_status, user_checkins, user_work_history, user_education_history, user_birthday, user_relationships, friends_checkins',
          'redirect_uri' => 'https://' . $_SERVER['HTTP_HOST'] . '/fb_redirect', 
      );

      $loginUrl = $facebook->getLoginUrl($params_login);
      $sessionURL = "<a href='" . $loginUrl . "' target='_top'>Login</a>";


      //this triggers fb dialog box when app request is sent and if user goes to certain page without logging in. works fine.

      if ($fb_user == 0 && arg(0) == 'page') {
        print "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        error_log("function fb(). JS redirect to LOGIN URL triggered..", 0); //loginUrl = " . $loginUrl

        //clear session variables to start over 
        unset($_SESSION);
        error_log("function fb(). clear session ", 0);

      }

    error_log("function fb(). NOT logged in.", 0); //loginUrl = " . $loginUrl
  }


  if ($fb_user) {

    try {

      $access_token = $facebook->getAccessToken();
      $facebook->setAccessToken($access_token);

      //Proceed knowing you have a logged in user who's authenticated.
      //Save facebook data as session variables
      $_SESSION['user_profile'] = $facebook->api('/me');
      $_SESSION['user_relationships'] = $facebook->api('/me/family'); 
      $_SESSION['user_friends'] = $facebook->api('/me/friends');
      $_SESSION['user_checkins'] = $facebook->api('/me/statuses');


      error_log("function fb(). fbuser = " . $fb_user . ". facebook user_profile->id = " . $_SESSION["user_profile"]["id"], 0);

      return array($_SESSION['user_profile'], $_SESSION['user_relationships'], $_SESSION['user_checkins'], $_SESSION['user_friends'], 1);


    } catch (FacebookApiException $e) {

      error_log("function fb(). " . $e);

    }
  }

  return array(false, false, false, false, false);
}

0 个答案:

没有答案