Using Facebook PHP SDK with CodeIgniter

This tutorial is for using the Facebook connect feature with CodeIgniter framework.

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 https://github.com/facebook/facebook-php-sdk/zipball/master 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.
 
<?php 
$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.

<?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 
  • 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()
{ 
    $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; 
        //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.

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 https://github.com/facebook/facebook-php-sdk/zipball/master 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.
 
<?php 
$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.

<?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 
  • 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()
{ 
    $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; 
        //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?

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.