0

I am using Facebook Login Button to integrate with facebook in my website. But, I don't know how to save the username etc(userdata on facebook) from facebook to my database on login through 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.

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 ?

<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>
4

1 に答える 1

1

facebookphpsdkをダウンロードしてください。

以下は、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>

ログイン部分でこのように確認してください

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
}
于 2012-04-16T12:24:07.830 に答える