开发者

Facebook PHP SDK, how to maintain persistent login

I'm using the non-JS example from the most recent FB PHP SDK. It works fine, except when a user closes their browser and re-opens the page. They have to click the FB login link again.

I was under the assumption $facebook->getUser() handles cookies.

Code:

       require 'sdk/facebook/src/facebook.php';
       $facebook = new Facebook(array(
      'appId'  => 'test',
      'secret' =开发者_如何学JAVA> 'test',
      'cookie' => true));


// Get User ID
$user = $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.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

?>

Is there an extra step I'm missing here? I found code here to pull out data from the FB cookies: facebook auto re-login from cookie php

Apparently that's not needed anymore with the SDK though?


It is not possible to do without client-side.

Your script just doesn't have enough information to know what user requested the page.

And it is not possible to do anything with cookies, since facebook session is a dynamic state that is stored on facebook servers. So the only possible way to use JS SDK, get the current session and use it on the server-side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜