• Call Us: +92-333-7276335
  • - Mail Us: info@shekztech.com

Plot 1177, Sector 31B - Crossing, Karachi, Sindh

Loading...
Login With Facebook using CodeIgniter
  • Views: 4.7K
  • Category: Codeigniter
  • Published at: 19 Feb, 2017
  • Updated at: 18 Aug, 2023

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 provides your email address and name, etc.

Nowadays, every person is busy, so why he/she fills out your form and validates the Email Address is a lengthy 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 will show you how to Log in with Facebook in ci.

What do 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) and Facebook.php(for library folder)

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

Upload the Facebook.php from the 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; go to your config folder autoload.php

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

Note: We are using Bootstrap for front-end layout, so if you don't know about Bootstrap, ignore classes names 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, paste this code into index function, and create a view home in the 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 your 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 purposes and paste this code.

 

public function signout(){
  //facebook library function.
  $this->facebook->destroy_session();
  //codeigniter function.
  $this->session->sess_destroy();
  redirect('login_with_fb/login');
}

https://www.youtube.com/watch?v=6KpDUf5M-ks

 

Shehzad Ahmed

Shehzad Ahmed is a highly qualified expert with a Master of Philosophy in Computer Science and a decade of extensive industry experience. With his impressive track record in web development and computer science, he has not only left an indelible mark on the industry but also made substantial contributions to education. Since , he has created more than eighty exhaustive courses, paving the way for innumerable individuals interested in learning and development. His unparalleled knowledge and innate ability to elucidate complex ideas make him a highly sought-after educator and consultant. Choose Shehzad and take advantage of his exceptional balance of technical expertise and teaching prowess to propel your learning journey or project to new heights.

0 Comment(s)
Write your comment