Google Login / Register script using codeigniter php framework | 2my4edge

18 July 2017

Google Login / Register script using codeigniter php framework

Here we are going to see about google or googleplus login using php codeigniter with mysql database. google login script is used to make register time very less. and people will use it for their registration and login purpose. in this script you will get email and some other personal information after successfully logged in. here we re going to check already existing users also in mysql database. lets see the code and work flow in below with installation process.

google login script in codeigniter with mysql


FLOW OF GOOGLE LOGIN

google login work flow diagram

CONFIG AND INSTALLATION VIDEO



STEP 2 : You need to mention the authorized redirect url and authorized javascript origin like site url.

STEP 3: Then get ready to config your codeigniter file, put into your htdocs or www folder in localhost or live server and configure all the files like autoload, config, database, etc.

STEP 4 : Add googleplus.php config file in application -> config folder.

STEP 5 : modify your api key, client id, client secret key and redirect url.

STEP 6 : Add google Library and googleplus.php files into your application->libraries folder.

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('googleplus');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{

$this->load->view('login');

}



public function google_login()
{
if (isset($_GET['code'])) {         
$this->googleplus->getAuthenticate();
$user = $this->googleplus->getUserInfo();
$checkemail = $this->db->query('select id,email 
              from users where email = "'.$user["email"].'"');
$emailresult = $checkemail->result_array();
if($emailresult[0]['email'] != $user["email"]){
$user_information  = array(
'name'           => $user["name"],
'first_name'     => $user["given_name"],
'last_name'      => $user["family_name"],
'email'          => $user["email"],
'gender'         => $user["gender"],
'source_id'      => $user["id"],
'source'         => 'Google',
'profilepicture' => $user["picture"],
);
$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'  => 'Google',
                   'profilepicture'  => $user["picture"]);
$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();
redirect('welcome/profile', $data);
} 
else 
{
echo 'We are unable fetch your google 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->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 :

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

PROFILE.PHP file is to show the user infromation.

WELCOME_MESSAGE.PHP / LOGIN.PHP file is for login link
<a href="<?php echo $this->googleplus->loginURL(); ?>">
    <span class="fa fa-google"></span> Sign in with Google
</a>

Hope this will be really helpful to you to make google / googleplus login in php codeigniter framework with mysql database. for video demo link on this link. Thank you.

No comments:

Post a Comment

^