Login with facebook using codeigniter - Mostlikers

27 April, 2015

Login with facebook using codeigniter

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

Login with facebook using codeigniter

Live demo                 Download

In this post i have explain 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 a application in facebook. After creating app facebook will provide your application app id and app secrete id.

  • Create a new website app
  • Go App review make 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';


Create a new controller for accessing login code. Here i have created 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    
        else :
            $data['login_url'] = $this->facebook->getLoginUrl(array(
                'redirect_uri' => site_url('welcome/flogin'), 
                'scope' => array("email") // permissions here


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 get the facebook access token data it will store database. If that user already register get the user information store on session redirect to 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