Login with Facebook in Codeigniter


Here we used PHP SDK 3.0 and JavaScript SDK for login with facebook in CodeIgniter for the process of authentication and authorizing users for your app.

For Creating an App, which you can obtain from the App Dashboard.
You need App Id and App Secret for integration of the facebook login in CodeIgniter.

Live DemoLive Demo

Download Script

In this CodeIgniter module we included the Facebook PHP SDK 3.0 in libraries of the application directory.And we define base url in config.php.
And We create one file which is facebook.php in config folder for the define of AppId and Appsecret.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['appID']	= 'Your APP Id';
$config['appSecret']	= 'Your App Secret';

We create one Controller which is fbci.php. In this controller, we used the PHP SDK for getting the facebook user data.Here we used the api() function which is PHP SDK function defined in facebook class.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'libraries/facebook/facebook.php';
class Fbci extends CI_Controller {
   public function __construct(){
	public function index()

	function logout(){
		header('Location: '.$base_url);
	function fblogin(){
		$facebook = new Facebook(array(
		'appId'		=>  $this->config->item('appID'),
		'secret'	=> $this->config->item('appSecret'),
		$user = $facebook->getUser();
				$user_profile = $facebook->api('/me');
				$params = array('next' => $base_url.'fbci/logout');
				   'logout' =>$facebook->getLogoutUrl($params)
				header('Location: '.$base_url);
			}catch(FacebookApiException $e){
				$user = NULL;

Here We create one view file which is located in application/views directory.In that view file we use the FB.login() function for authentication which is JavaScript SDK function.

<?php  $this->load->helper('html');?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Facebook</title>
<?php  $base_url=$this->config->item('base_url'); ?>
<script src="<?php echo $base_url;?>js/jquery.min.js" type="text/javascript"></script>
if(empty($ses_user))   { 
echo img(array('src'=>$base_url.'images/facebook.png','id'=>'facebook','style'=>'cursor:pointer;float:left;margin-left:550px;'));
 }  else{
 echo '<img src="https://graph.facebook.com/'. $ses_user['id'] .'/picture" width="30" height="30"/><div>'.$ses_user['name'].'</div>';	
	echo '<a href="'.$this->session->userdata('logout').'">Logout</a>';
<div id="fb-root"></div>
   <script type="text/javascript">
  window.fbAsyncInit = function() {
       appId:'<?php echo $this->config->item('appID'); ?>', cookie:true, 
       status:true, xfbml:true,oauth : true 
           var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
           if (d.getElementById(id)) {return;}
           js = d.createElement('script'); js.id = id; js.async = true;
           js.src = "//connect.facebook.net/en_US/all.js";
           ref.parentNode.insertBefore(js, ref);
 $('#facebook').click(function(e) {
    FB.login(function(response) {
	  if(response.authResponse) {
		  parent.location ='<?php echo $base_url; ?>fbci/fblogin';
 },{scope: 'email,read_stream,publish_stream,user_birthday,user_location,user_work_history,user_hometown,user_photos'});


  1. nice……….script

  2. Kate willson

    Thanks for the script..
    but can i also be provided with the code i.e to how to store user info into one’s database

  3. i am getting following problem

    Severity: Notice

    Message: Use of undefined constant CURLOPT_IPRESOLVE – assumed ‘CURLOPT_IPRESOLVE’

    Filename: facebook/base_facebook.php

    Line Number: 952

  4. In my site i implement this script and working fine but logout url not working and that redirect to my facebook home page so any body have any idea how to solve this probelm.

    • For the logout, in the controller under fblogin() function, I used

      $params = array(‘next’ => $base_url.’fbci/logout’);
      ‘logout’ =>$facebook->getLogoutUrl($params)

      In the above code, the redirect url for logout should be defined in $params variable(‘next’ is nothing but redirect url) and generate a url for logout using the $facebook->getLogoutUrl($params). This logout url variable stored in the session. In the view files, read the session logout variable and use in hyperlink.

  5. nicely explained

  6. Hi
    great work ,but i have a problem , after successful login with i am not getting user information

    $user_profile = $facebook->api(‘/me’);
    after calling this function php code stuck and i am not getting the reason why this is happening ( check your demo that was working perfectly)

  7. tinhphaistc

    cURL support enabled
    cURL Information libcurl/7.24.0 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    Severity: Notice

    Message: Use of undefined constant CURL_IPRESOLVE_V4 – assumed ‘CURL_IPRESOLVE_V4′

    Filename: facebook/base_facebook.php
    help me, please BIG THANKS

  8. Matthew

    Hi, very useful article.
    I have a question : how to call friend list?
    Thanks in advance.

  9. Nilesh


  10. Waja Nejla

    I have downloaded the zip, i have tested the application but I have this error :

    Fatal error: Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:\wamp\www\fbci\fbci\application\libraries\facebook\base_facebook.php:19 Stack trace: #0 C:\wamp\www\fbci\fbci\application\libraries\facebook\facebook.php(18): require_once() #1 C:\wamp\www\fbci\fbci\application\controllers\fbci.php(9): require_once(‘C:\wamp\www\fbc…’) #2 C:\wamp\www\fbci\fbci\system\core\CodeIgniter.php(248): include(‘C:\wamp\www\fbc…’) #3 C:\wamp\www\fbci\fbci\index.php(201): require_once(‘C:\wamp\www\fbc…’) #4 {main} thrown in C:\wamp\www\fbci\fbci\application\libraries\facebook\base_facebook.php on line 19

Leave a Reply