2012-02-25 155 views
0

更新:通過臉書登錄連接2

這裏是用戶點擊我的註冊Facebook的按鈕發送到。

<?php 
define('FACEBOOK_APP_ID', 'XXXX'); 
define('FACEBOOK_SECRET', 'XXXX'); 

function parse_signed_request($signed_request, $secret) { 
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data 
    $sig = base64_url_decode($encoded_sig); 
    $data = json_decode(base64_url_decode($payload), true); 

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { 
    error_log('Unknown algorithm. Expected HMAC-SHA256'); 
    return null; 
    } 

    // check sig 
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); 
    if ($sig !== $expected_sig) { 
    error_log('Bad Signed JSON signature!'); 
    return null; 
    } 

    return $data; 
} 

function base64_url_decode($input) { 
    return base64_decode(strtr($input, '-_', '+/')); 
} 

if ($_REQUEST) { 
    $response = parse_signed_request($_REQUEST['signed_request'], 
             FACEBOOK_SECRET); 

$uname = $response["registration"]["username"]; 
$rname = $response["registration"]["name"]; 
$seckey = $response["registration"]["seckey"]; 
$email = $response["registration"]["email"]; 
$password = $response["registration"]["password"]; 
$gender = $response["registration"]["gender"]; 
$ip_last = $_SERVER['REMOTE_ADDR']; 
$time = time(); 
$sessionKey = 'RevCMS-'.rand(9,999).'/'.substr(sha1(time()).'/'.rand(9,9999999).'/'.rand(9,9999999).'/'.rand(9,9999999),0,33); 
$newgender = substr("$gender", -4, 1); 
$newpass = md5($password); //This will encrypt the password with md5, if you're not using RevCMS ensure your CMS uses md5 or change it. 
$newseckey = md5($seckey); //This will encrypt the security key with md5. 
// Connecting to database 
mysql_connect('localhost', 'root', 'habbo123'); 
mysql_select_db('phoenix3'); 

// Inserting into users table 
$result = mysql_query("INSERT INTO users (id, username, real_name, password, mail, auth_ticket, rank, credits, vip_points, activity_points, activity_points_lastupdate, look, gender, motto, account_created, last_online, online, ip_last, ip_reg, home_room, respect, daily_respect_points, daily_pet_respect_points, newbie_status, is_muted, mutant_penalty, mutant_penalty_expire, block_newfriends, hide_online, hide_inroom, mail_verified, vip, volume, seckey) 
VALUES 
(NULL, '$uname', '$rname', '$newpass', '$email', '$sessionKey','3', '10000', '0', '1000', '0', '-', '$newgender', 'I <3 Tropical-Resort', '$time', '$time', '0', '$ip_last', '$ip_last', '8', '0', '3', '3', '0', '0', '0', '0', '0', '0', '0', '1', '0', '100', '$newseckey')"); 
if($result){   
//Begin redirect to logged in page 
header('Location:http://tropical-resort.org/me'); 
// End redirect 
} 
else 
{ 
//Un-hash this code if you're getting errors, if the error is to do with mysql it will tell you whats wrong. (Be sure to hash the redirect if you un-hash this.) 
#echo mysql_error(); 
#echo "<br>"; echo "\"$newgender\""; 
// Heres the redirect page, if you like you could send them to an error page because they'll only be sent this link if there was an error registering them. 
header('Location:http://tropical-resort.org/index'); 
} 
} 
else 
{ 
//$_REQUEST was left empty, this means at least 1 field was blank however it's more than likely they tried to go direct to this page. 
?><script>alert("You left a field out, Please click back and make sure to fill everything in.")</script> 
<form> 
<input type="button" value="Go Back" onClick="history.go(-1);return true;"> 
</form> 
<?php 
} 

?> 

我需要什麼,現在得到完成的就是添加一個按鈕,我的索引,上面寫着登錄,如果他們點擊它,它應該嘗試登錄到我的網站與他們的Facebook,如果用戶不具有帳戶它應該將它們重定向到我的註冊腳本,否則它應該自動登錄並重定向到登錄屏幕。

信息,你可能需要知道:

密碼字段登錄使用MD5加密

任何幫助,將不勝感激,如果你需要任何更多的信息只是問:)

-Brad


在我剛纔的問題facebook registration connect我要求幫助建立我的註冊頁面。那麼我現在已經成功完成了,但現在我需要做到這一點,所以用戶也可以用facebook登錄。要做到這一點,我希望使用FaceBook登錄按鈕,如果他們點擊登錄,並且它檢測到他們沒有註冊,它將重定向到註冊頁面,否則它將登錄他們。

我將使用什麼代碼(示例) ?你需要知道任何特殊信息?

+0

可能重複的[Facebook在PHP網站集成](http://facebook.stackoverflow.com/questions/7775079/facebook-integration-in-a-php-site) – DMCS 2012-02-25 22:23:47

回答

1

看看這篇文章,其中涵蓋了你想要實現的目標。 Facebook Integration in a PHP site

+0

我發現很混亂,我我要用我的註冊碼更新我的帖子,然後如果可能的話,你能否告訴我如果要在用戶註冊時添加到索引中以便讓它登錄。感謝:) – 2012-02-25 22:23:35

+1

我認爲你應該考慮在進入代碼之前將其分解成邏輯。檢查Facebook會話(甚至可以在Facebook Connect JS上執行此操作,而無需在PHP中執行任何操作)。如果會話存在,驗證會話以確保您可以檢索用戶的訪問令牌,然後使用它來查找他們的UID(可以對https://graph.facebook.com/me/?fields=id&access_token= { access_token},看看你的UID是否存在於你的用戶表中,如果它存在,則它們被註冊並進入認證方法,如果它不存在,它們就不會被註冊並處理它 – 2012-02-25 22:32:01

+0

我理解邏輯謝謝,但是我不太清楚我會如何去實際編碼...我願意付出的幫助看到,因爲這其實很重要! – 2012-02-25 22:35:05