title: Codeigniter Facebook Login fails
body: <p>I tried several solutions from stackoverflow for this problem but no one helps, so I am opening a new one and hope someone can help me find a solution.</p>
  
  <p>The <strong>Facebook PHP API</strong> is used for <strong>Login to a Webportal</strong>, but I do not get any user details after the user clicks on the login link and accepts the scope.<br>
  <code>$this-&gt;facebook-&gt;getUser()</code> always returns <code>0</code>.</p>
  
  <p>The Facebook PHP files facebook.php, base_facebook.php and fb_ca_chain_bundle.crt are stored in <code>application/libraries/facebook/</code>.</p>
  
  <h2>Facebook Configuration file application/config/development/facebook.php</h2>
  
  <pre><code>$config['credentials'] = array(
    'appId' =&gt; 'the_app_id',
    'secret' =&gt; 'the_app_secret',
    'sharedSession' =&gt; true,
    'trustForwarded' =&gt; true,
    'cookie' =&gt; true
  );
  
  $config['login'] = array(
    'domain' =&gt; 'test.domain.com',
    'scope' =&gt; 'email',
    //this url is also defined in the facebook app for Login Action
    'redirect_uri' =&gt; 'http://test.domain.com/login/doLogin?facebook' 
  );
  </code></pre>
  
  <h2>View application/view/index/loginForm.php</h2>
  
  <p>The view contains the link to the Facebook authentication controller.</p>
  
  <pre><code>...
  
  &lt;a href="&lt;?= base_url('login/doLogin?facebook') ?&gt;" class="btn facebook"&gt;
    Facebook Login
  &lt;/a&gt;
  
  ...
  </code></pre>
  
  <h2>Controller application/controllers/login.php</h2>
  
  <p>The function <code>doLogin</code> is checking for the requested login method and calling the corresponding function which handles the authentication.</p>
  
  <pre><code>public function doLogin() {
    $is_login = FALSE;
  
    //Facebook Login
    if ($this-&gt;input-&gt;get('facebook') !== FALSE) {
  
    //set get params to $_REQUEST Array for internal checks of the facebook library
    //because Codeigniter handles the input by it's own class input
    $_REQUEST = array(
      'code' =&gt; $this-&gt;input-&gt;get('code'),
      'state' =&gt; $this-&gt;input-&gt;get('state')
    );
  
    $is_login = $this-&gt;_facebook_login();
    } elseif (){
      //other Login Method
    }
  
    ... 
    //do some other stuff depending on $is_login e.g. redirects user to the correct url or show error
  }
  
  protected function _facebook_login() {
    $this-&gt;load-&gt;config('facebook');
    $fb_cred = $this-&gt;config-&gt;item('credentials');
    $this-&gt;load-&gt;library('facebook/facebook', $fb_cred);
  
    $user = $this-&gt;facebook-&gt;getUser();
  
    if ($user) {
      try {
        $user_profile = $this-&gt;facebook-&gt;api('/me');
      } catch (FacebookApiException $e) {
        $user = null;
        log_message('error', $e);
        return FALSE;
      }
      var_dump($user_profile);
      return TRUE;
    }
  
    //User is not logged in via Facebook, so redirect him to the Facebook Login page
    $fb_login_conf = $this-&gt;config-&gt;item('login');
    $loginUrl = $this-&gt;facebook-&gt;getLoginUrl($fb_login_conf);
  
    redirect($loginUrl);
  }
  </code></pre>
  
  <h2>Login Steps</h2>
  
  <ol>
  <li>After clicking on the Facebook Login Link the user is redirected to Facebook.  </li>
  <li>There the user is logging in to Facebook and accepting the scope.</li>
  <li>Then the user is redirected to the correct redirect URL.</li>
  <li>The function <code>_facebook_login</code> should now get a token from the code and with the token the email of the user should be in <code>$user_profile</code>.</li>
  </ol>
  
  <hr>
  
  <p>Edited Code according to curious_coder suggestion.<br>
  Now the browser returns an error <code>Error 310 (net::ERR_TOO_MANY_REDIRECTS)</code></p>
  
  <hr>
  
  <p>A try without looking for user </p>
  
  <pre><code>$this-&gt;load-&gt;config('facebook');
  $fb_cred = $this-&gt;config-&gt;item('credentials');
  $this-&gt;load-&gt;library('facebook/facebook', $fb_cred);
  
  $user = $this-&gt;facebook-&gt;getUser();
  
  try {
    $user_profile = $this-&gt;facebook-&gt;api('/me');
    var_dump($user_profile);
  } catch (FacebookApiException $e) {
    $user = null;
    log_message('error', $e);
    return FALSE;
  }
  </code></pre>
  
  <p>is throwing the following error message in Apache Log</p>
  
  <pre><code>[error] [client ***.***.***.***] CSRF state token does not match one provided., referer: http://test.domain.com/login/doLogin?facebook
  </code></pre>
  
  <p>and in Codeigniter Log</p>
  
  <pre><code>OAuthException: An active access token must be used to query information about the current user.
  </code></pre>