Facebook认证到我的PHP页面


Facebook Authentication to my PHP Page

我正在尝试允许用户使用Facebook在我的页面上注册。这就是我如何连接/登录到Facebook(只是JS代码)。

function statusChangeCallback(response) {
    if (response.status === 'connected') {
        // Logged into your app and Facebook.
        FB.api('/me', function(response) {
            var reg_Name = response.first_name;
            var reg_Surname = response.last_name;
            var reg_Email = response.email;
            //$.post("System/External.php?function=login_facebook")
        });
        console.log(response);
    } else if (response.status === 'not_authorized') {
        // The person is logged into Facebook, but not your app.
        alert('Please log into this application to continue.');
    } else {
        // The person is not logged into Facebook, so we're not sure if
        // they are logged into this app or not.
        alert('Please log into Facebook to continue.');
    }
}
function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
}
window.fbAsyncInit = function() {
    FB.init({
        appId      : 'app_key',
        cookie     : true,  // enable cookies to allow the server to access
        // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v2.0' // use version 2.0
    });
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
};
// Load the SDK asynchronously
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

我的问题是我想登录后,它已注册。我用$。post请求注册,但现在什么将是最好的方式来验证实际登录,因为你不能检索用户的FB密码?

我希望页面执行实际登录身份验证(从我的网站),当用户登录到Facebook。例子:

将有2种方法登录到我的网站。

  1. 使用我的登录界面(当用户名和密码是正确的)。
  2. 使用Facebook(然后应该将数据发布到相同的PHP页面,通过我的网站登录-所以这将是如果用户通过我的网站登录)。

我的第一次尝试是使用个人电子邮件登录到网站(但是如果有人获得他们的电子邮件,用户帐户很容易被黑客攻击)。我不希望用户在已经验证了他的Facebook帐户后还必须输入密码。

我不能使用Facebook电子邮件和IP地址的组合,因为你的IP每次连接到互联网时都会改变。(不需要密码登录)

下面是成功登录后Facebook的响应。

email: "my_email address"
first_name: "My Name"
gender: "male"
id: "1508539292713828"
last_name: "My Surname"
link: "https://www.facebook.com/app_scoped_user_id/1508539292713828/"
locale: "en_US"
name: "Full name"
timezone: 2
updated_time: "2014-06-26T08:21:36+0000"
verified: true

您可以使用PHP SDK在服务器端检查用户是否登录:

// initialize SDK here
// If this returns the user ID, the user is logged in
if ($userId = $facebook->getUser()) {
    try {
        $userData = $facebook->api('/me');
    } catch (Exception $e) {
        echo $e->getMessage();
        exit();
    }
    $email = $userData["email"];
    // Do whatever
}

Edit:流程将是:使用JS SDK登录用户,然后在FB中发送AJAX请求。login回调,检查用户是否使用PHP SDK登录。