title: Cannot login through Facebook
body: <p>Here is the code of API calls and login through Facebook. 
  The problem is that when it redirects to Facebook, it does not sign in.
  What part of my code is wrong and how can I fix it?</p>
  
  <pre><code>require 'facebook/facebook.php';
  require 'config/fbconfig.php';
  require 'config/functions.php';
  $url = $_REQUEST['lasturl'];
  
  $facebook = new Facebook(array(
     'appId' =&gt; APP_ID,
            'secret' =&gt; APP_SECRET,
            'cookie' =&gt; true
      ));
  
  $session = $facebook-&gt;getSession();
  
  if (!empty($session)) {
      # Active session, let's try getting the user id (getUser()) and user info (api-&gt;('/me'))
  try {
      $uid = $facebook-&gt;getUser();
      $user = $facebook-&gt;api('/me');
  } catch (Exception $e) {
  
  
  }
  
      if (!empty($user)) {
          # User info ok? Let's print it (Here we will be adding the login and registering routines)
         // echo '&lt;pre&gt;';
          //print_r($user);
         // echo '&lt;/pre&gt;&lt;br/&gt;';
          $username = $user['name'];
          $user = new User();
          $userdata = $user-&gt;checkUser($uid, 'facebook', $username);
          if(!empty($userdata)){
              session_start();
              $_SESSION['id'] = $userdata['id'];
              $_SESSION['oauth_id'] = $uid;
  
              $_SESSION['username'] = $userdata['username'];
              $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
              header("Location: ".$url);
          }
      } else {
          # For testing purposes, if there was an error, let's kill the script
          die("There was an error.");
      }
  } else {
      # There's no active session, let's generate one
      $login_url = $facebook-&gt;getLoginUrl();
      header("Location: " . $login_url);
  }
  </code></pre>