title: Codeigniter V2.x with FB API 4
body: <p>I want to create a simple facebook web app using CodeIgniter to be able to post to a page linked to my facebook account.</p>
  
  <p>Currently this is my code:</p>
  
  <p>Controller:</p>
  
  <pre><code>function facebook(){
      $this-&gt;config-&gt;load('facebook', TRUE);
      $this-&gt;load-&gt;library('facebook');
      $login_url = $this-&gt;facebook-&gt;get_user();
      echo $login_url;
  }
  </code></pre>
  
  <p>Libraries:</p>
  
  <p>I literally use the code pasted here: <a href="http://www.benmarshall.me/facebook-sdk-php-v4-codeigniter/" rel="nofollow">http://www.benmarshall.me/facebook-sdk-php-v4-codeigniter/</a> (by Ben Marshall).</p>
  
  <p>The library code, by default does not print appropriate Exception. So i had to extend the code so it looks like this (on the constructor):</p>
  
  <pre><code>public function __construct() {
          $this-&gt;ci =&amp; get_instance();
          $this-&gt;ci-&gt;load-&gt;library('session');
          if (!isset($_SESSION)){
              session_start();
          }
  
          FacebookSession::setDefaultApplication(
              $this-&gt;ci-&gt;config-&gt;item('api_id', 'facebook'),
              $this-&gt;ci-&gt;config-&gt;item('app_secret', 'facebook')
          );
  
          $this-&gt;helper = new FacebookRedirectLoginHelper($this-&gt;ci-&gt;config-&gt;item('redirect_url', 'facebook'));
          if($this-&gt;ci-&gt;session-&gt;userdata('fb_token')){
              $this-&gt;session = new FacebookSession( $this-&gt;ci-&gt;session-&gt;userdata('fb_token'));
  
              // Validate the access_token to make sure it's still valid
              try {
                  if (!$this-&gt;session-&gt;validate()){
                      $this-&gt;session = false;
                  }
              }catch(Exception $e){
                  // Catch any exceptions
                  $this-&gt;session = false;
              }
  
          }else{
              try {
                  $this-&gt;session = $this-&gt;helper-&gt;getSessionFromRedirect();
              }catch(FacebookRequestException $ex){
                  echo "&lt;pre&gt; "; print_r($ex-&gt;getCode());
                  // When Facebook returns an error
              }catch(Exception $ex){ 
                  echo "&lt;pre&gt;"; print_r($ex);
                  // When validation fails or other local issues
              }
          }
  
          if ($this-&gt;session ){
              $this-&gt;ci-&gt;session-&gt;set_userdata( 'fb_token', $this-&gt;session-&gt;getToken() );
              $this-&gt;session = new FacebookSession( $this-&gt;session-&gt;getToken());
          }
      }
  </code></pre>
  
  <p>What i did is:
  1. I manually start the session, because otherwise i would get an error saying " Session not active, could not load state." With Error Code: 721.</p>
  
  <p>However, even if i had start the session, FacebookRedirectLoginHelper always returns NULL.</p>
  
  <p>After i dig a little deeper, it turns out within the FacebookRedirectLoginHelper.php, specifically loadState() function always returns NULL.</p>
  
  <p>This is the code for loadState()</p>
  
  <pre><code>protected function loadState()
    {
      if ($this-&gt;checkForSessionStatus === true
        &amp;&amp; session_status() !== PHP_SESSION_ACTIVE) {
        throw new FacebookSDKException(
          'Session not active, could not load state.', 721
        );
      }
      if (isset($_SESSION[$this-&gt;sessionPrefix . 'state'])) {
        $this-&gt;state = $_SESSION[$this-&gt;sessionPrefix . 'state'];
        return $this-&gt;state;
      }
  
      return null;
    }
  </code></pre>
  
  <p>Can anyone suggest a possible solution for me? Has anyone successfully develop FB API 4 with CI?</p>