title: Error Facebook application, permission accept but...doesn't work at first time
body: <p>I have a big problem about my database e the permission of my application in facebook.</p>
  
  <p>when the user open the app (index.php) and accept the permission, the app go to another page (game.php)... when the game.php is open, the script check in the database to look if the ID of facebook is already present, if not the database make the user.</p>
  
  <p>when the game.php is open at first time, the page generate this error:</p>
  
  <pre><code>OAuthException: An active access token must be used to query information about the current user.
  Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in game.php on line 67 check1Error: Duplicate entry '0' for key 'UIDfacebook' account
  </code></pre>
  
  <p>maybe because the ID of facebook is not ready for save in the database, and why? the permission was accept... if I refresh the page, the page will work fine and the database save the ID, username....</p>
  
  <p>INDEX.PHP (REQUEST)</p>
  
  <pre><code>&lt;?php
  require('client/facebook.php');
  
  $app_id = '***';
  $app_secret = '***';
  $canvas_page = "https://apps.facebook.com/***";
  $canvas_url = "****";
  
  $code = $_REQUEST['code'];
  
  if(!$code){
  $display= 'page';
  $scope= 'offline_access, publish_stream, user_about_me, user_likes, email'; 
  $redirect_url = 'game.php';
  $oauth_url    = 'https://www.facebook.com/dialog/oauth?canvas=1&amp;client_id='.$app_id.'&amp;display='.$display.'&amp;redirect_uri='.urlencode($redirect_url).'&amp;scope='.$scope;
  $config = array('appId'  =&gt; $app_id,'secret' =&gt; $app_secret,'cookie' =&gt; true,'domain' =&gt; true);
  $facebook_client = new Facebook($config);
  
  echo "&lt;script type=\"text/javascript\"&gt;top.location.href = \"".$oauth_url."\";&lt;/script&gt;";
  }
  ?&gt;
  </code></pre>
  
  <p>GAME.PHP</p>
  
  <pre><code>&lt;?php
  require 'client/facebook.php';
  require('client/databaselogin.php');
  ///////////////////
  $app_id = "***";
  $secret = "***";
  $app_url = "https://apps.facebook.com/***/";
  //Create our Application instance
  $facebook = new Facebook(array(
  'appId' =&gt; $app_id,
  'secret' =&gt; $secret,
  'cookie' =&gt; 'true',
  ));
  // Get User ID
  $user = $facebook-&gt;getUser(); 
  //Do we have a user?
  if(!$user) {
      $loginUrl = $facebook-&gt;getLoginUrl(array('redirect_uri'=&gt; $app_url));
      //redirect the url to prompt for login and permissions
      } else {
          try{
              //Proced knowing you have a logged in user who's autenticated
              $user_profile = $facebook-&gt;api('/me');
              $name = $user_profile['name'];
              $id = $user_profile['id'];
              $first_name = $user_profile['first_name'];
              $last_name = $user_profile['last_name'];
              $link = $user_profile['link'];
              $username = $user_profile['username'];
              $gender =  $user_profile['gender'];
              $locale = $user_profile['locale'];          
          } catch (FacebookApiException $e) {
              echo ($e);
              $user = null;
          }
  }
  
  
  $checkUIDdb = "";
  $connection = "wait";
  
  $id = $user_profile['id'];
  $username = $user_profile['username'];
  $goldDB = "0";
  $user_type = "A";
  
  ///connect to the database
  
  $connessione = mysql_connect($host, $userdb, $passdb)
          or die("error: " . mysql_error());
      print ("connect");
  
      echo $connection = "ok";
      echo $id;
  
  ///
  
  $fb_id = $user_profile['id'];
  $locale = $user_profile['locale'];
  $con=mysqli_connect($sdb,$userdb,$passdb,$db);
  
  if ($connection == "ok") 
  {
  //Checking User Data @ WT-Database
  $check1_task = "SELECT * FROM `tabletest` WHERE `UIDfacebook` = " . $fb_id . " LIMIT 0, 30 ";
  $check1_res = mysqli_query($con, $check1_task);
  $checken2 = mysqli_fetch_array($check1_res);
  print $checken2;
  
  //If User does not exist @ WT-Database -&gt; insert
  if (!($checken2)) {
  $add = "INSERT INTO tabletest (id,UIDfacebook, username, goldDB, user_type) VALUES ('', '$fb_id', '$username', '$goldDB', '$user_type')";
  mysqli_query($con, $add);
  if (!$check1_res) {
      echo "check1";
      printf("Error: %s\n", mysqli_error($con));
  }
  } 
  //Double-check, User won't be able to load app on failure inserting to database 
  if (!($checken2)) {
  
      //echo "Excuse us " . $first_name . ". Something went terribly wrong! Please try again later!";
  
  } 
  }
      //Close off the MySQL connection to save resources.
   ?&gt;
  
  
  &lt;head&gt;
  &lt;/head&gt; 
  &lt;/body&gt;
      &lt;/script&gt;
      MY APPLICATION
      &lt;/script&gt;
  &lt;/body&gt; 
  &lt;/html&gt; 
  </code></pre>
  
  <p>somebody know what is happen? </p>
  
  <p>Thank you very much</p>
  
  <p>EDIT:</p>
  
  <p>I try to use this script for the GAME.PHP:</p>
  
  <pre><code>&lt;?php
  require 'client/facebook.php';
  $app_id = "aaa";
  $secret = "aaa";
  $app_url = "http://app.facebook.com/aaa/";
  
  // Create our Application instance
  $facebook = new Facebook(array(
  'appId' =&gt; $app_id,
  'secret' =&gt; $secret,
  ));
  
  // Get User ID
  $user = $facebook-&gt;getUser(); 
  
  //Do we have a user?
  if(!$user) {
      $loginUrl = $facebook-&gt;getLoginUrl(array('redirect_uri'=&gt; $app_url));
      //redirect the url to prompt for login and permissions
      echo "&lt;script type='text/javascript'&gt;top.location.href = '$loginUrl';&lt;/script&gt;";
      } else {
          try{
              //Proced knowing you have a logged in user who's autenticated
              $user_profile = $facebook-&gt;api('/me');
              $name = $user_profile['name'];
              $id = $user_profile['id'];
              $first_name = $user_profile['first_name'];
              $last_name = $user_profile['last_name'];
              $link = $user_profile['link'];
              $username = $user_profile['username'];
              $gender =  $user_profile['gender'];
              $locale = $user_profile['locale'];
  
  
          } catch (FacebookApiException $e) {
              echo ($e);
              $user = null;
          }
  }
  echo $username;
  echo $id;
  echo $user_profile;
  ?&gt;
  &lt;!doctype html&gt;
  &lt;html xmlns:fb="http://www.facebook.com/2008/fbml"&gt;
  &lt;head&gt;
  &lt;title&gt;php-sdk example1 &lt;/title&gt;
  &lt;style&gt;
  body {
      font-family:Arial, Helvetica, sans-serif;
  }
  a{
      text-decoration:none;
  }
  </code></pre>
  
  <p>but at the first time, the error is similar?</p>
  
  <pre><code>OAuthException: Error validating access token: The session was invalidated explicitly using an API call.
  </code></pre>
  
  <p>if i refresh the page, the problem disappears....</p>