Login With Facebook Using PHP CodeIgniter

Feb 10, 2019

Facebook login is simply an open authentication that is used for providing access to the server data after authorizing a user via third party. In this tutorial, we are going to explain Login With Facebook Using PHP CodeIgniter. how you can implement Facebook login via open authentication to connect with your website using php CodeIgniter.

You can achieve this login using Facebook SDK in PHP.

First Download the Facebook PHP SDK and save it in your CodeIgniter’s libraries folder.

Step 1: Now generate your Facebook app id and Facebook app secret key from https://developers.facebook.com and sign in by an existing Facebook username or password.

Below is our complete code with short explanation :

Step 2: Go to the config folder (located at the root of the CI installation) and create a new facebook.php” file.

now that App ID and App Secret Key you have to add into your application code as shown below:

$config['facebook_app_id']              = 'YOUR-APP-ID';
$config['facebook_app_secret']          = 'YOUR-APP-SECRET';
$config['facebook_login_type']          = 'web';
$config['facebook_login_redirect_url']  = 'facebook_login';
$config['facebook_logout_redirect_url'] = 'facebook_login/logout';
$config['facebook_permissions']         = array('email');
$config['facebook_graph_version']       = 'v2.6';
$config['facebook_auth_on_load']        = TRUE;

Step 3: Go to the controller folder (located at the root of the CI installation) and create a new facebook_login.php” file.

defined('BASEPATH') OR exit('No direct script access allowed');

class facebook_login extends CI_Controller {

    function __construct() {

    public function index() {
        $data['fb'] = $this->getauth();
        $data['LogonUrl'] =  $this->facebook->login_url();
        $this->load->view('fb_login', $data);

    public function getauth() {
        $userProfile = array();
        if ($this->facebook->is_authenticated()) {
            $userProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,email,gender,locale,picture');
        return $userProfile;

    public function logout() {

Step 4: Go to the view folder (located at the root of the CI installation) and create a new fb_login.php” file.

  <title>Login with Facebook</title>
<?php if (isset($fb['id']) && !empty($fb['id'])) { ?>
        <a href="<?php echo base_url('facebook_login/logout') ?>">Logout from facebook</a>
        <?php } else { ?>
        <a href="<?php echo $LogonUrl ?>"><img src="<?php echo base_url('assets_signi/flogin.png') ?>"></a>

        <?php } ?>

        // print_r($fb); 
        if (isset($fb['id']) && !empty($fb['id'])) {
                <table class="table">
                        <td>Id: </td>
                        <td><?php echo $fb['id'] ?></td>
                        <td>Name: </td>
                        <td><?php echo $fb['first_name'] . ' ' . $fb['last_name'] ?></td>
                        <td>Profile Pic</td>
                        <td><img src="<?php echo $fb['picture']['data']['url']; ?>"></td>
                        <td>Email: </td>
                        <td><?php echo $fb['email'] ?></td>
<?php } ?>

Copyright 2018. All rights are reserved