Login with facebook using codeigniter - Mostlikers

27 April, 2015

Login with facebook using codeigniter

Nowadays most of the website registration having the Facebook login system. Because users get boring to fill all the form information. Usually, they will not log out with facebook and other social media account. So I would suggest to you use facebook user login. The Just user will click button facebook share they are information.

Login with facebook using codeigniter

Live demo                 Download

In this post, I have explained about Login with facebook using CodeIgniter. here I am using facebook SDK to get access user information to store database and get the user details.

Facebook App

You have to create an application on facebook. After creating app facebook will provide your application app id and app secret id.

  • Create a new website app
  • Go App review makes it live configuration.
  • Get App ID and App Key
  • Download latest SDK file


Create sample facebook user table in your database.
CREATE TABLE IF NOT EXISTS `facebook_user` (
`id` int(11) NOT NULL,
  `email` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `f_id` int(11) NOT NULL

Facebook SDK 2.0

Download facebook SDK file and mymodel libraries file add in your libraries folder. Create new file config folder facebook.php add below code that file.

Path : applications/config/facebook.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    $config['appId']   = 'XXXXXXXXX';
    $config['secret']  = 'XXXXXXX';

Controller (facebook login welcome home page)

Create a new controller for accessing login code. Here I have created a welcome controller.

Path : applications/controllers/welcome.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {

    public function __construct()

    public function index()
        $this->data['login_url'] = $this->facebook->getLoginUrl(
        array('redirect_uri' => site_url('welcome/flogin'),'scope' => array("email")));

    public function flogin()
        $user = "";
        $userId = $this->facebook->getUser();
        if ($userId) {
            try {
                $user = $this->facebook->api('/me');
            } catch (FacebookApiException $e) {
                $user = "";
        else {
            echo 'Please try again.'; exit;
        if($user!="") :
           echo '<pre>'; print_r($user); exit;  
           //Write here login or register script
           // Check the email id exist or not    
        else :
            $data['login_url'] = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url('welcome/flogin'), 
                'scope' => array("email") // permissions here

Facebook email id permission

In case you didn't get any email id. Use below code.
$user = $this->facebook->api('/me'); 
$user = $this->facebook->api('/me?fields=email,first_name,last_name');


Create login.php in view folder add below code.
Path : applications/views/login.php
<div id="container">
    <h1>Login with facebook</h1>
    <div id="body"> 
    <h2>Welcome to home page</h2>   
    <a href="<?= $login_url;?>">
     <img src="<?php echo base_url(); ?>resource/images/facebook.png">

After getting the facebook access token data it will store database. If that user had already registered get the user information store on session redirect to a profile page.

Thank you for visiting.if you have any quires comment here.

"Once you replace negative thoughts with positive ones, you'll start having positive results."
                                         - Willie Nelson

Related Topics


  1. i will bee glad if you provide twitter also ?

    1. is it possible to make it become one table between login facebook and twitter ?

    2. Yes is it possible. Create login_type colum enaum ('fb','twit') users table. Store the user information based on fb,twitter

    3. Hi bro after research your blog just insert new user, how about users already insert it ? where should i check first !

    4. Check this line if(!$this -> users ->count_all_results('f_id',$user['id']))

  2. Hello again bro, first of all thanks for making this actually i'm not checking your file i just search and search then i came back again !

    let me tell you my pain based on you script ya ?

    $this->load->model('user_model','users'); //it seems like alias ya ?

    then you use your model as users, am i right ? please correct then

    your model
    $this -> table = 'facebook_user';
    $this -> result_mode = 'object';

    actually what this model ?

    did you want to make all become easy to proses all data in databse ya ?

    piuh so hard to handle this lah

    Thanks man, my pain still keep on !

    when the script login and logout with twitter ?

    LOL to mush request and asking, forgive me

  3. bro how can i just use default model in codeignter ? so hard use your model in my script, i'm not familiar with your model, how can i use default model in codiegnter, what should remove and how tpo fix with simple model, thanks a lot

  4. Hi, thanks ! am using your code. But insertion is not happening. It redirect to the login same page after the facebook sign in.

  5. hi how do i implement this script in hmvc ? also how do i i direct my own button to this script?

    1. https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

  6. Replies
    1. First check the facebook api response.

  7. It shows me error like Invalid App ID: 0

  8. session is not expiring even after logout?

  9. Replies
    1. Make sure your app id have access to email permission. Or change the latest versions sdk file.

  10. it is working fine but it is show user name and id , i want email id
    how will find out.

    thank u

    1. Just change few line.

      $user = $this->facebook->api('/me');

      $user = $this->facebook->api('/me?fields=email,first_name,last_name');

  11. Hello,

    I am getting user id as 0

    Any olution