在注册表格中使用facebook选项添加登录

时间:2014-11-11 11:44:41

标签: php facebook facebook-graph-api facebook-javascript-sdk

我的网站上有一个注册表单,用户提交他们的个人详细信息,如姓名,电子邮件ID,dob和手机号码,以存储在我的数据库中。现在我想通过使用该链接包含连接Facebook按钮,用户可以通过登录他们的帐户来提供他们的详细信息。这些细节可以存储在我的数据库中。我的期望看起来像这个图像..帮助我实现这一点  enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您需要决定是否要将PHP或JavaScript用于Facebook的SDK。然后,从以下位置下载SDK:

https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.2

这是"入门" PHP指南:

https://developers.facebook.com/docs/php/gettingstarted/4.0.0

这里是JavaScript的同一类指南:

https://developers.facebook.com/docs/javascript

这是我使用PHP编写的Facebook登录代码的一部分。请注意,我使用CodeIgniter作为我项目的框架:

public function registerWithFacebook()
{
        try {

        // Proceed knowing you have a logged in user who's authenticated
        $facebook_user_data = $this->facebook->get_user();

        } catch (FacebookApiException $e) {
            $user = null;
            print_r($e);
            return false;
        }

        // if we don't have the facebook-user array variable, leave the method
        if (!$facebook_user_data) {
            return false;
        }

        // If we can't get the email, leave the method
        if(!$facebook_user_data['email']) {
            return false;
        }

        // If user is already registered with Facebook, start sessions and redirect user to app home page (exit the method at this point)
        if ($this->facebook_model->facebookUserIdExistsAlreadyInDatabase($facebook_user_data)) {

            $session = $this->user_model->getUserInfo($facebook_user_data['id']);

                $data = array(
                    'user_id'           => $session['user_id'],
                    'name'              => $session['name'],
                    'email'             => $session['email'],
                    'profilepic'        => $session['profilepic'],
                    'loggedin'          => true,
                    );

                $this->session->set_userdata($data);    
                redirect(base_url().'home');
                exit();

        }


        // Generate password for new user since you can't get the password from Facebook (obviously)
        $generated_password = random_string('alnum', 8);

        $data = [
        'email'                 => $facebook_user_data['email'],
        'name'                  => $facebook_user_data['name'],
        'password'              => $this->phpass->hash($generated_password),
        'profilepic'            => 'https://graph.facebook.com/'.$facebook_user_data['id'].'/picture?width=160&height=160',
        'user_facebook_id'      => $facebook_user_data['id'],

        ];


        // Insert data to your database
        $new_user = $this->facebook_model->add_facebook_user($data);

        // If new user's data was saved successfully to database, start sessions and redirect user
        if($new_user) {

                $session = $this->user_model->getUserInfo($facebook_user_data['id']);

                $data = array(
                    'user_id'           => $session['user_id'],
                    'name'              => $session['name'],
                    'email'             => $session['email'],
                    'profilepic'        => 'https://graph.facebook.com/'.$facebook_user_data['id'].'/picture?width=160&height=160',
                    'loggedin'          => true,
                    );

                $this->session->set_userdata($data);

                redirect(base_url().'home');
        }
}