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

Generate your website

Do you know that the PHP Code Generator can generate PHP websites in minutes in CodeIgniter 3?

Learn more about our PHP Code Generator and generate your web applications using PCG with functioning admin portal.

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.