Facebook login / Register script using codeigniter php framework | 2my4edge

06 July 2017

Facebook login / Register script using codeigniter php framework

Today we are going to see facebook login and register script, If the user is new, then register, If the user is already in database then just login, This facebook login script is very simple to use, facebook third party login is used to make register very easy and simple, and by using this script we are getting email also, in most of the site's scripts are not providing email id while doing facebook login. let me explain the script and installation also.

facebook login with codeigniter and mysql
DOWNLOAD          LIVE DEMO

USE OF FACEBOOK LOGIN

facebook login with codeigniter and mysql structure



CONFIG AND INSTALLATION DEMO



STEP 2 : After Creating Facebook APP. Make the APP Public.

STEP 3 : In htdocs or www folder create a project folder. After that configure your codeigniter project. For demo, please refer our demo video, there i have explained you. (autoload,config,database etc.. files)

STEP 4 : Add facebook.php file into application->config folder.

STEP 5 : Enter your facebook app id and secret key code.

STEP 6 : Add facebook library files in application->libraries folder (Facebook folder and Facebook.php file).

STEP 7 : CONTROLLER

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

public function __construct()
{
parent::__construct();
$this->load->library('facebook');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{
$this->load->view('welcome_message');
}



public function facebook_login()
{
if ($this->facebook->is_authenticated())
{
$user = $this->facebook->request('get', '/me?fields=id,name,email,gender,picture');
//echo '<pre>';
//print_r($user); exit();
if (!isset($user['error']))
{
$data['user'] = $user;
$checkemail = $this->db->query('select id,email 
    from users where email = "'.$user["email"].'"');
$emailresult = $checkemail->result_array();
if($emailresult[0]['email'] != $user["email"]){
$names =  explode(" ", $user['name']);
$user_information  = array(
'name'           => $user["name"],
'email'          => $user["email"],
'first_name'     => $names[0],
'last_name'      => $names[1],
'gender'         => $user["gender"],
'source_id'      => $user["id"],
'source'         => 'Facebook',
'profilepicture' => 'https://graph.facebook.com/'.$user["id"].'/picture?type=large',  
);
$this->welcome->insert_user($user_information);
$insert_id = $this->db->insert_id();
$fetchuser = $this->db->query('select * from users where id = "'.$insert_id.'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}else if($emailresult[0]['email'] == $user["email"]){
$update_id = array('source_id'  => $user["id"],
   'source'  =>'Facebook',
   'profilepicture'  => 'https://graph.facebook.com/'.$user["id"].'/picture?type=large');
$this->db->where('id', $emailresult[0]['id']);
$this->db->update('users', $update_id);
$fetchuser = $this->db->query('select * from users where id = "'.$emailresult[0]['id'].'"');
$userdata = $fetchuser->result_array();
$this->session->set_userdata ( 'user_id', $userdata[0]['id'] );
$this->session->set_userdata ( 'user_name', $userdata[0]['name'] );
$this->session->set_userdata ( 'user_email', $userdata[0]['email'] );
$this->session->set_userdata ( 'user_gender', $userdata[0]['gender'] );
$this->session->set_userdata ( 'user_source', $userdata[0]['source'] );
$this->session->set_userdata ( 'user_source_id', $userdata[0]['source_id'] );
}

$data['userprofile'] = $this->session->userdata();
//echo "<pre>";
//print_r($data['userprofile']);
redirect('welcome/profile', $data);

}

}
else 
{
echo 'We are unable fetch your facebook information.'; exit;
}

}



public function profile()
{
if($this->session->userdata('user_id') != ''){
$this->data['userprofile']= $this->welcome->fetch_user();
$this->load->view('profile', $this->data, FALSE);
}else{
redirect('welcome', $data);
}
}


public function logout()
{
$this->facebook->destroy_session();
$this->session->sess_destroy();
redirect(base_url(), redirect);
}



}


STEP 8: MODEL

Welcome_model.php
<?php
class Welcome_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}


public function insert_user($data){
$this->db->insert('users', $data); 
return TRUE;
}

public function fetch_user(){
$query=$this->db->query("SELECT *
     FROM users
     WHERE id = '".$this->session->userdata('user_id')."' ");
//return $query->result();
return $query->row(0);
}






}

STEP 9 : VIEWS

1.profile.php
2.welcome_message.php / login.php

PROFILE.PHP file is for display user information.

WELCOME_MESSAGE.PHP file is for login link.
<a class="btn btn-facebook" href="<?php echo $this->facebook->login_url(); ?>">
    Sign in with Facebook
</a>

Hope this will be useful to you. Try our demo. Thanks for visiting our site. keep visiting for more updates. 

5 comments:

  1. I am nno longer sure where you are getting your info, however great
    topic. I needs to spend a while finding out much more or understanding more.
    Thanks for great info I was searching for this info forr my mission.

    ReplyDelete
  2. This is actually pretty interesting. Never thought of facebook login/register with codeigniter php framework - I am really excited to try it out. Your blog is totally one of a kind. Thank you for posting.

    ReplyDelete
  3. On profile page I am getting error saying:
    Fatal error: Call to a member function login_url() on null
    can you please help me with this error

    ReplyDelete
  4. thanks for this instruction, it's sure to be useful, how can you do this on a smartphone?

    ReplyDelete
  5. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.. Buy Bad Facebook Reviews

    ReplyDelete

^