2012-08-09 118 views
1

當用戶通過增強型身份驗證對話框驗證我的應用程序,並將其重定向到畫布頁時,我的畫布頁是否知道其首次訪問?facebook身份驗證後首次訪問

+0

你不得不在FB用戶ID保存到自己的數據庫 - 那麼你可以,如果看它用戶訪問您的畫布頁面之前一直在那裏。 – CBroe 2012-08-09 12:43:21

+0

讓我重新說明我的問題:有沒有一種方法可以讓我知道用戶通過單擊允許來到增強型身份驗證對話框的我的應用程序頁面? – SavaMinic 2012-08-10 12:38:45

回答

1

你可能會從Facebook用戶ID後的用戶重定向到畫布頁: 這裏是例子:

require_once("facebook.php"); 

$config = array(); 
$config[‘appId’] = 'YOUR_APP_ID'; 
$config[‘secret’] = 'YOUR_APP_SECRET'; 
$config[‘fileUpload’] = false; // optional 

$facebook = new Facebook($config); 

$app->user_id = $facebook->getUser(); 

你獲得USER_ID後,你可以將它保存在數據庫中,這樣就可以知道用戶是否第一次訪問。

0

上一個答案是正確的,這裏是我用來檢查他們是新用戶還是返回用戶的代碼。然後,如果它們是新的,我將它們插入到數據庫中。

// Get the app User ID 
$user = $facebook->getUser(); 


$seeif = mysql_num_rows(mysql_query("SELECT id FROM users WHERE fbid='$user' LIMIT 1")); 

if ($seeif == "1") { 
// already exists 
} else { 
// insert new user 
mysql_query("INSERT INTO users (fbid) VALUES('$user') ") 
or die(mysql_error()); 
} 
0

雖然以前的答案是正確的,但從長遠來看構建應用程序時,這可能對您更有用。

記住這依賴於你具有開放的數據庫連接,以及Facebook的PHP SDK

// this brings back more info print_r($user) 
$user = $facebook->api('/me'); 
$exists = mysql_num_rows(mysql_query("SELECT id FROM users WHERE fbid='" . $user['id'] . "')); 

if ($exists > 0) { 
    // already exists 
} else { 
    // insert new user 
    mysql_query("INSERT INTO users (fbid) VALUES('" . $user['id'] . "') ") or die(mysql_error()); 

    // do something else (post to timeline?) 
    // look up facebook feed for more info on posting 
try { 
    // Post Message to feed. 
    $facebook->api('/me/feed', 'POST', $msg); 
} catch (FacebookApiException $e) { 
    error_log($e); 
    } 
}