Login With Facebook using CodeIgniter

Login with Facebook using Codeigniter

Why login with Social networks..? If a person/user wants to create an account on your system/website/web application he/she provide your email address and name etc.

Nowadays every person is busy so why he/she fill your form and validate the Email Address, it is a long procedure to create an account your user/customer can create an account in your system by pressing a single button OR your user/customer can log in with a single click.

Today I am going to show you how to Log in with Facebook in ci.

what you need..?

PHP 5.4+
CodeIgniter 3
CodeIgniter session library
Facebook PHP SDK v5
Composer
Register your application on Facebook

 

Download the composer and install it in your windows/Linux/ we are using windows.

Download CodeIgniter3

Now download and install Facebook PHP SDK v5 with the composer.

Download files, these are two files facebook.php(config file) Facebook.php(for library folder)

In your Codeigniter /application/config/config.php file set $config[‘composer_autoload’] to TRUE

Upload the Facebook.php from library folder to your application/library folder

Upload the facebook.php config file in /application/config/facebook.php with your Facebook app detail.

$config['facebook_app_id'] = 'Your app id';
$config['facebook_app_secret'] = 'Your app secret';
$config['facebook_login_type'] = 'web';
$config['facebook_login_redirect_url'] = 'login_with_fb'; //login url
$config['facebook_logout_redirect_url'] = 'login_with_fb/login'; //logout url
$config['facebook_permissions'] = array('public_profile', 'publish_actions', 'email');
$config['facebook_graph_version'] = 'v2.6';
$config['facebook_auth_on_load'] = TRUE;

 

 

Set Facebook+session libraries and URL helper to autoload simply go to your config folder autoload.php

Create a view called login and paste this code in this view

Note: We are using Bootstrap for front-end layout so if you don’t know about bootstrap ignore classes name in divs.

 

<!DOCTYPE html>
<html>
<head>
    <title>Demo | Login with facebook in codeigniter</title>
</head>
<body>
    <?php echo anchor($fburl,'Login with Facebook','class="xyz"')?>
</body>
</html>

 

Now Create a controller named Login_with_fb and create a Function login paste this code in your login Function.

 

public function login(){
 if($this->session->has_userdata('ulogin'))//we are checking if user is logedin
 {
    redirect('login_with_fb');//redirect if user logedin
 }


 if ($this->facebook->is_authenticated())//checking if the user is authenticate
 {
    $userProfile = $this->facebook->request('get', '/me?fields=id,first_name,last_name,picture,email,gender');
    if (!isset($userProfile['error']))
    {
      $fbdata = array();//setting an array variable 
      $fbdata['ulogin'] = TRUE; //assigning a value
      $fbdata['user_data'] = $userProfile; //user info from facebook
      $this->session->set_userdata($fbdata);//setting session in ci
      if($this->session->userdata('ulogin'))//checking if the session is set.
      {
       redirect('login_with_fb');//redirect if the session is set
      }
      else
      {
       echo 'Your custom error to login';//error
      }
    }
    else
    {
     $this->facebook->destroy_session();
     redirect('login_with_fb');
    }
 }
 else
 {
   $fbuser = '';
   $data['fburl'] = $this->facebook->login_url();//fatching facebook url if the user not logedin
   $data['title'] = "Log in with in Codeigntier | shakzee";//for titlet in html
   $this->load->view('login',$data);//setting view sending data in $data varaible

 }

 }//function ends here

 

Now create a Function index and paste this code in index function and also create a view home in past view code in your home.php view.

 

function index(){
  if($this->session->has_userdata('ulogin'))
  {
   $data['alldata'] = $this->session->userdata('user_data');//setting userdata in $data['alldata'] variable
   $this->load->view('home',$data);
  }
  else
  {
   redirect('login_with_fb/login');
  }

 }

 

paste this code in you home.php view

<!DOCTYPE html>
<html>
<head>
    <title>Demo | Login with facebook in codeigniter</title>
</head>
<body>
    <?php
         var_dump($alldata).br(1);
         anchor('login_with_fb/signout','Logout','class="xyz"')

         echo 'Wecome'. $alldata['first_name'].nbs(1).$alldata['last_name'];
         echo 'First Name:' . $alldata['first_name'];
         echo 'Last Name:' . $alldata['first_name'];
         echo 'Email:' . $alldata['first_name'];
         echo 'Gender:' . $alldata['first_name'];
         echo img($alldata['picture']['data']['url');// autoload html helper before using img() function 

    ?>
</body>
</html>

 

Create a function signout in our controller for logout purpose paste this code.

 

public function signout(){
  //facebook library function.
  $this->facebook->destroy_session();
  //codeigniter function.
  $this->session->sess_destroy();
  redirect('login_with_fb/login');
}
shakzee
administrator
My Name is Shehzad Ahmed and I am a full-time developer with over 7+ years of experience. In that time, I’ve worked as a developer with many big and small firms and was teaching web design and development skills to many keen learners and by the time passes I’ve found a great passion and potential on myself as a teacher on this particular field and now I am a full-time teacher and working as a freelancer.

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp chat