Facebook身份验证到我的PHP页面

时间:2014-08-05 13:59:43

标签: php facebook authentication

我试图允许用户使用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页面 - 所以就像用户通过我的网站登录一样)。
  3. 我的第一次尝试是使用人员电子邮件登录该网站(但如果有人收到他们的电子邮件,则很容易被用户帐户攻击)。我不希望用户在已经验证其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
    

1 个答案:

答案 0 :(得分:1)

您可以使用服务器端的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
}

编辑:流程是:使用JS SDK记录用户,然后在FB.login回调中发送AJAX请求,在那里检查用户是否使用PHP登录SDK。