2011-08-15 75 views
8

我使用最新FB PHP SDK中的非JS示例。它工作正常,除非用戶關閉瀏覽器並重新打開頁面。他們必須再次點擊FB登錄鏈接。Facebook PHP SDK,如何保持持久登錄

我是在假設$ facebook-> getUser()處理cookie。

代碼:

 require 'sdk/facebook/src/facebook.php'; 
     $facebook = new Facebook(array(
     'appId' => 'test', 
     'secret' => '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(); 
} 

?> 

是否有額外的步驟,我在這裏失蹤?我發現這裏的代碼從FB cookies中提取數據:facebook auto re-login from cookie php

顯然這不再需要SDK了嗎?

回答

2

沒有客戶端是不可能的。

您的腳本沒有足夠的信息來知道用戶請求該頁面。

由於facebook會話是存儲在Facebook服務器上的動態狀態,因此無法對cookie執行任何操作。所以使用JS SDK的唯一可能的方式是獲取當前會話並在服務器端使用它。