繁体   English   中英

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时,我希望页面执行实际的登录身份验证(来自我的网站)。 例:

有两种方法可以登录到我的网站。

  1. 使用我的登录界面(用户名和密码正确时,这将登录您)。
  2. 使用Facebook(然后将数据发布到与通过我的网站登录相同的PHP页面上-就像用户通过我的网站登录一样)。

我的第一个尝试是使用“个人电子邮件”登录到该网站(但是,如果有人获得了他们的电子邮件,则很容易会入侵用户帐户)。 我不希望用户在验证其Facebook帐户后不必输入密码。

我不能同时使用Facebook电子邮件地址和IP地址,因为每次您连接到Internet时,您的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
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM