简体   繁体   English

使用Facebook登录按钮插件将Facebook与网站集成,并将用户凭证存储到数据库

[英]Facebook integration with website using facebook login button plugin and store user crenditials to database

I am using Facebook Login Button to integrate with facebook in my website. 我正在使用Facebook 登录按钮与我的网站中的facebook集成。 But, I don't know how to save the username etc(userdata on facebook) from facebook to my database on login through facebook . 但是,我不知道如何在通过facebook登录时将用户名etc(facebook上的userdata)从facebook保存到我的数据库。 How this can be done ? 如何做到这一点?

Currently, when clicking the facebook login button an window is opened, we can enter the facebook username and password and it will be connected. 当前,单击facebook登录按钮时,将打开一个窗口,我们可以输入facebook用户名和密码,它将被连接。

How to enter into php part while login to store user datas on facebook and if already exist login to site without store any data ? 如何在登录时进入php part以在Facebook上存储用户数据,以及如果已经存在登录到站点而不存储任何数据?

<div id="fb-root"></div>
<script>(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/all.js#xfbml=1&appId=987654321";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>


   // facebook login button

<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1" ></div>

Please download the facebook php sdk. 请下载facebook php sdk。

Below is an example for connecting to facebook. 以下是连接到Facebook的示例。

<?php session_start();
require 'src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

// See if there is a user from a cookie
$user = $facebook->getUser();
//$session = $facebook->getSession();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    //echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}
?>

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { 
    foreach($user_profile as $skey => $sval)
    {
        $_SESSION["$skey"]=$sval;   
    }   
    $_SESSION['fb_name']=$user_profile['name']; //get facebook username and set it in session
    $_SESSION['fb_id']=$user_profile['id']; //get facebook id and set it in session
    $logoutUrl = $facebook->getLogoutUrl();
    $_SESSION['fb_logout']= $logoutUrl;

     ?>
      <pre>            
        <?php //print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre> 
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script>               
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>', 
          cookie: true, 
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
    window.location.href="index.php";
          //window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>

In login portion check like this 在登录部分检查像这样

if(isset($_SESSION['fb_id'])) {
//check whether the entry for this fb_id already exists in db
//if not exists insert id and name into db, else get username from db for this fb_id
}

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

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