Twitter login / Signup script using codeigniter php framework and mysql | 2my4edge

26 July 2017

Twitter login / Signup script using codeigniter php framework and mysql

Twitter login or signup script is also a one of the simple way to make people register in your website very simple. most of the twitter login scripts are not providing email. but here we are fetching email address also in this script. and also we are going check exists users in mysql database. lets see the code and work flow and script installation process.

login with twitter in codeigniter

FLOW OF TWITTER LOGIN

login with twitter in codeigniter flow diagram
CONFIG AND INSTALLATION VIDEO



STEP 1 : We have to create a twitter app to make twitter login. CREATE APP LINK

STEP 2 : We have to mention callback url while creating twitter login app.

STEP 3 : Config your codeigniter in localhost or server. then configure it. like autoload, config, database files etc.

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

STEP 5 : Change your consumerid and consumersecrect key and redirect url as you given in app creation.

STEP 6 : Add twitter library files in application -> libraries folder.

STEP 7 : CONTROLLER
<?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('twitter');
$this->load->config('twitter');
$this->load->helper('url');
$this->load->model('welcome_model','welcome'); 
}


public function index()
{

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

}




public function twitter_login() {
$ci = get_instance(); // CI_Loader instance
$ci->load->config('twitter');
include_once APPPATH."libraries/Twitter/twitteroauth.php";
$consumerKey = $ci->config->item('consumerKey');
$consumerSecret = $ci->config->item('consumerSecret');
$oauthCallback =  $ci->config->item('oauthCallback');
$sessToken = $_REQUEST['oauth_token'];
$sessTokenSecret = $consumerSecret;

if(isset($_REQUEST['oauth_token'])){
//Successful response returns oauth_token, 
    //oauth_token_secret, user_id, and screen_name
$connection = new TwitterOAuth($consumerKey, 
                      $consumerSecret, $sessToken, $sessTokenSecret); 
$accessToken = $connection->getAccessToken($_REQUEST['oauth_verifier']);
$params = array('include_email' => 'true', 
'include_entities' => 'false', 
'skip_status' => 'true');
$user = $connection->get('account/verify_credentials', $params);

$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->screen_name,
'last_name'       => '',
'location'        => $user->location,
'email'           => $user->email,
'gender'          => $user->gender,
'source_id'       => $user->id,
'source'          => 'Twitter',
'profilepicture'  => str_replace('_normal','',$user->profile_image_url),
);
$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'  => 'Twitter',
'profilepicture'  => str_replace('_normal','',$user->profile_image_url),
'location'  => $user->location);
$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 twitter 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 : VIEW

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


PROFILE.PHP is to show fetched data from database.

LOGIN.PHP / WELCOME_MESSAGE.PHP is login link page.

<a href="<?php echo $this->twitter->loginURL(); ?>">
   <span class="fa fa-twitter"></span> Sign in with Twitter
</a>

Hope you liked this post, this might be very helpful to make twitter login easy with fetch email also. for video demo or installation check this link. Thank you.

No comments:

Post a Comment

^