Using Facebook PHP SDK (v.3.1.1) with Codeigniter

This is a step by step tutorial for Codeigniter users for using the Facebook connect feature in their websites.

This is a step by step tutorial for Codeigniter users for using the Facebook connect feature in their websites. For the users who generates Codeigniter websites using PHP Code Generator" (PCG) the Facebook connect library for CodeIgniter library is already included in the library folder in the downloaded website.s

  • Step 1: Download the Facebook PHP SDK at and extract it. Copy the files in the src folder.
  • Step 2: Create a folder with name "facebook" in Codeigniter application libraries folder. Place the files from step 1 in the facebook folder.
  • Step 3: Create a configuration file with name facebook.php for storing the application id and secret key in application/config folder. The file should look like below.
$config['appId'] = 'Your Facebook App ID'; 
$config['secret'] = 'Your Facebook secret key';

  • Step 4: In the libraries folder create a file with name "fb_connect.php" and add the following code to that.

class Fb_connect extends Facebook
    //declare public variables public 
    $user = NULL; 
    public $user_id = FALSE; 
    public $fb = FALSE; 
    public $fbSession = FALSE; 
    public $appkey = 0; 

    //constructor method.
    public function __construct() 
        $CI = &get_instance(); 
        $config = $CI->config->item('facebook');

        $this->user_id = $this->getUser(); // New code
        $me = null; 
        if ($this->user_id)
                $me = $this->api('/me'); 
                $this->user = $me;
            catch (FacebookApiException $e) 

// end class 

  • Step 5: Now add the following functions to a Controller. You can have this code in your user controller if you have one already, or create a new controller and add. If you have added to "user" controller then login with facebook link can be generated by placing site_url('user/loginByFacebook') in your view file.

function loginByFacebook()

function facebook()
    if (!$this->fb_connect->user_id) 
        //Handle not logged in,
        $fb_uid = $this->fb_connect->user_id; 
        $fb_usr = $this->fb_connect->user; 
        //Handle user logged in,by updating session 
        //print_r($fb_usr) will help to see what is returned 

By now you should have a working implementation of Facebook connect. If you have difficulties implementing the Facebook connect in CodeIgniter website, we recommend to use PHP Code Generator where you get a working implementation with role based authentication and admin portal etc.

Website in minutes

Do you know that you can generate a complete PHP websites in minutes with PHP Website Generator?


  • Free admin dashboard
  • Role based security
  • Login with Facebook, Google and more
  • Queue based email processing
  • Price at $3 for website
  • Uses CodeIgniter framework

Generate website now See Pricing

Connect with Google or Facebook and generate your next CodeIgniter website in minutes with plenty of features like searching with pagination, AJAX based grid data editors, group based security and admin portal etc.