Connect with Facebook in CI

Guide to implement Connect with Facebook for your website.

Note: This post is very old, please use Facebook SDK4 at for implementing Connect with Facebook or for using Facebook graph API in your websites.

Follow the below simple steps and implement Connect with Facebook in your website.

  1. Create a Config file in application/facebook.php with the following code in it.
$config['appId'] = 'Your Facebook App ID'; $config['secret'] = 'Your secret Code';
  1. There is two files provided starting from V.3.0.0 of Facebook PHP SDK. Create a folder in application library and name it as facebook.
  2. Download the Facebook PHP SDK files and place it in the folder application/library/facebook. You can download it from
  3. Create a new file and name it as fb_connect.php in application/library and copy the following code inside that folder.

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 

Now create a Controller and name it as user, and write the following methods in it.

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; 
        //Hanlde user logged in, you can update your session with the available data         
        //print_r($fb_usr) will help to see what is returned 

You are done with coding now. Now when ever user need to login with facebook just redirect them to user/loginByFacebook and the login will handled automatically by Facebook. But you need to update the facebook() method with the actions success and failure conditions.

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 $5 for website
  • Uses CodeIgniter framework

Generate website now See Pricing

You can generate an entire website in CodeIgniter using our PHP Website Generator using easy to use wizards. PHP Website Generator provide many features including search with pagination, AJAX based grid data editors, Group based security and free Admin dashboard etc. Please connect with Google or Facebook and to generate your CodeIgniter website in minutes.