Make sure Facebook user "register" before login

If I put the Login button on my site, the user will be able to just log in - and actually do not need to register, How do I make sure the "user" actually REGISTER (so I can save the data in my database) before he can actually log in?

my code for index...

<?php

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '*id trimmed*',
  'secret' => '*secret trimmed*',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

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) { ?>
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
    <?php } else { ?>
      <fb:login-button>Logga in med Facebook</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.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>

Answers


Just compare the user id from facebook with your database, if exists -> login, else -> register:

// ...
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;
  }
}

if ($user)
{
  $id = $user_profile['id']; // facebook user id;
  // now use this $id to compare with your database saved user facebook user id
  // if not exists, then create user, else update login counter or only get profile
}
//...

Need Your Help

Embedding QWidget into X11 Window

c++ qt x11 xlib

I want to embed two QWidgets into a window created using XLib. I have written this code:

Splitting a string by a character

c++ string algorithm

I know this is a quite easy problem but I just want to solve it for myself once and for all