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 https://github.com/facebook/facebook-php-sdk-v4 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 https://github.com/facebook/php-sdk
  3. Create a new file and name it as fb_connect.php in application/library and copy the following code inside that folder.

<?php 
include(APPPATH.'libraries/facebook/facebook.php'); 
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(); 
        $CI->config->load("facebook",TRUE); 
        $config = $CI->config->item('facebook'); 
        parent::__construct($config); 
        $this->user_id = $this->getUser(); // New code 
        $me = null; 
        if ($this->user_id) 
        { 
            try 
            {
                $me = $this->api('/me'); 
                $this->user = $me; 
            } 
            catch (FacebookApiException $e) 
            {
                error_log($e); 
            }
        }
    } 
} // end class 

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


function loginByFacebook()
{
    $this->load->library('fb_connect'); 
    $param['redirect_uri']=site_url("user/facebook"); 
    redirect($this->fb_connect->getLoginUrl($param)); 
} 

function facebook()
{
    $this->load->library('fb_connect');
    if (!$this->fb_connect->user_id) 
    { 
        //Handle not logged in,
    } 
    else 
    { 
        $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?

Features

  • 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.