Facebook登录 - 使会话持续更长时间

时间:2015-09-18 03:49:05

标签: php facebook session

我使用public function loginFb($facebookId) { $result = $this->db->mysqli->prepare("SELECT id, fb_id FROM users WHERE fb_id = ?"); $result->bind_param('i', $facebookId); $result->execute(); $result->store_result(); $result->bind_result($id, $facebookId); if($result->num_rows > 0) { $soon = array(); while ($result->fetch()) { $soon['id'] = $id; $soon['fb_id'] = $facebookId; } $soonSess = serialize($soon); $_SESSION["user"] = $soonSess; $_SESSION["login_time"] = time(); $_SESSION["logged_in"] = 1; $result->close(); return true; }else{ return false; } } 将用户登录到我的网站。大约30分钟后,用户必须再次登录。我该怎么做才能扩展用户的会话或扩展facebook令牌...不确定需要做什么。

ini_set('display_errors', 'on');
ini_set('session.cookie_httponly', 1);
//ini_set('session.use_only_cookies',1);
ini_set('session.cookie_lifetime', 86400);
ini_set('session.gc_maxlifetime', 86400);

ini_set('session.gc_probability', 0);
ini_set('session.gc_divisor', 1000);

我还对会话使用以下规则:

//initialize Facebook
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FAcebookRedirectLoginHelper($redirect_url);

try {
    $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
    // When Facebook returns an error
    die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
    // When validation fails or other local issues
    die(" Error : " . $ex->getMessage());
}

//check if facebook session exists
if(isset($_SESSION['fb_token']))
{
    $session = new FacebookSession($_SESSION['fb_token']);
}

if(isset($session))
{
    $_SESSION['fb_token'] = $session->getToken();

    try
    {
        $user = (new FacebookRequest(
            $session, 'GET', '/me'
        ))->execute()->getGraphObject(GraphUser::className())->asArray();

    } catch(FacebookRequestException $e) {

    }

    $name = $user['name'];
    $firstName = $user['first_name'];
    $lastName = $user['last_name'];
    $fbId = $user['id'];
    $fbEmail = $user['email'];
}

当用户退出Facebook帐户时,会话是否会过期?或者,使用Facebook登录时,使用寿命更长的最佳做法是什么?

编辑**

TruncatedNormal('gamma_own_%i_' % i, mu=go, tau=v_gamma_inv, value=0, a=-np.inf, b=0)

1 个答案:

答案 0 :(得分:1)

您可以通过调用

将短期用户访问令牌(有效2小时)扩展为长期用户访问令牌(有效期为60天)
GET /oauth/access_token?  
    grant_type=fb_exchange_token&           
    client_id={app-id}&
    client_secret={app-secret}&
    fb_exchange_token={short-lived-token}