title: How to check Facebook User's ID already exists before lauching app?
body: <p><strong>Short description.</strong> I'm developing flash game for <em>Facebook</em>. I need to show registration form first time when user launching application. It should check If user already registered (existing in database).</p>
  
  <hr>
  
  <p><strong>Purpose.</strong> To achieve this I need to get <em>Facebook User's ID</em> and check in database if <em>Facebook User ID</em> already exsists, before any providing any action.</p>
  
  <hr>
  
  <p><strong>Question.</strong> How to get <em>Facebook User's Id</em> and send it to registration.php? Should I have seperate files for checking user's existance, opening app, getting <em>Facebook User's Id</em>?</p>
  
  <hr>
  
  <p><strong>What I've tried:</strong> For now I've tried something like that (some explaination in code's comments):</p>
  
  <p><em>FacebookPermissions.php</em></p>
  
  <pre><code> include_once "php-sdk/src/facebook.php";
   $app_id = 'xxxxx';
   $app_url = 'http://www.facebook.com/myPage/app_xxxxx?ref=ts';
   $facebook = new Facebook(array(
    'appId'  =&gt; 'your app id',
    'secret' =&gt; 'your app secret'
   ));
  
   $user = $facebook-&gt;getUser();
   if ($user) {
  
        // here should go code to check database with Facebook User's ID
        // I don't have ideas how to achieve It correctly
  
        $user_profile = $facebook-&gt;api('/me');
        $id = $user_profile['id'];
        session_start();
        $_SESSION['id'] = $id;
  
        // here should redirect to php file where is MySql query for checking?
        // if yes, how to redirect It correctly?
  
   }
   else
   {
         // this part is to ask permissions for user
  
         $loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" . 
         $app_id . "&amp;redirect_uri=" . urlencode($app_url) . "&amp;scope=email";
         echo("&lt;script&gt; top.location.href='" . $loginUrl  . "'&lt;/script&gt;");
   }
  </code></pre>
  
  <p>This is php script to get data and from form and insert It to database, but It should be used only If user not exists in database. And at all form should be shown only in that case (if user not exists).</p>
  
  <p><em>InsertData.php</em></p>
  
  <pre><code>&lt;?php 
  session_start();
  $id = $_SESSION['id']; 
  $first_name = $_SESSION['first_name']; 
  $last_name = $_SESSION['last_name']; 
  $city = $_SESSION['city']; 
  $email = $_SESSION['email']; 
  
  $mysqli = new mysqli("localhost","xxx","xxx","xxx");
  if ($stmt = $mysqli-&gt;prepare("INSERT INTO users (FB_Id, First_Name, Last_Name, City, Email) VALUE (?,?,?,?,?) ")) 
  {
      if (!$mysqli-&gt;set_charset("utf8")) 
      {
          printf("Error loading character set utf8: %s\n", $mysqli-&gt;error);
      } 
      $stmt-&gt;bind_param('sssss', $id, $first_name, $last_name, $city, $email);
  
      $stmt-&gt;execute();
  
      if ($stmt-&gt;error != '') 
      {
         echo ' error:'.$stmt-&gt;error;
      } else 
      {
         echo 'success';
      }
      $stmt-&gt;close();
  } else 
  {
     echo 'error:'.$mysqli-&gt;error;
  }
  $mysqli-&gt;close();
  </code></pre>