For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Login with OTP in CodeIgniter

Overview

In this example, we will discuss the Login example using CodeIgniter.

We will use five files for this example:

  1. Signin.php (application\controllers\Signin.php)
  2. User.php (application\controllers\User.php)
  3. sign_in.php (application\views\sign_in.php)
  4. verify.php (application\views\verify.php)
  5. Signin_model.php (application\controllers\Signin_model.php)

Database [user_info] Table

CREATE TABLE `user_info` ( 
`id` INT(10) NOT NULL AUTO_INCREMENT , 
`name` VARCHAR(255) NOT NULL , 
`mobile` VARCHAR(255) NOT NULL , 
`otp` VARCHAR(255) NOT NULL , 
PRIMARY KEY (`id`)
) ENGINE = InnoDB;

application/controllers/Signin.php

class Signin extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('Signin_model');
	}

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

	public function login() {
		$mobile		= $this->input->post('mobile');
		
		$user 		= $this->login_model->check_mobile($mobile);

		if($user) {

			// Generate OTP
			$otp = $this->generate_otp();

			$data = [
				'otp'	=> $otp,
			];

			// update otp in database
			$this->login_model->update_otp($mobile, $data);

			// send otp on mobile number
			$message = $otp." is your OTP. Do not share with anyone.";

			$this->send_sms($mobile, $message);

			$data['mobile'] = $mobile;
			$this->load->view('otp', $data);	

		} else {
			echo "Invalid mobile number";
		}
	}

	public function send_sms($phone, $body) {

		// Your authentication key
		$authKey 	= 'auth_key';

		// Multiple mobiles numbers separated by comma					
		// Sender ID,While using route4 sender id should be 6 characters long.
		$senderId 	= 'CXSTEC';

		// Your message to send, Add URL encoding here.
		$message 	= urlencode($body);

		//Define route 
		$route 		= 'trans';

		//Prepare you post parameters
		$postData 	= array(
			'authkey' 	=> $authKey,
			'mobiles' 	=> $phone,
			'message' 	=> $message,
			'sender' 	=> $senderId,
			'route' 	=> $route
		);	

		//API URL
		$url 		= 'http://api.msg91.com/api/sendhttp.php';	

		$ch = curl_init();
		curl_setopt_array($ch, array(
			CURLOPT_URL 			=> $url,
			CURLOPT_RETURNTRANSFER		=> true,
			CURLOPT_POST 			=> true,
			CURLOPT_POSTFIELDS 		=> $postData
			));		

		//Ignore SSL certificate verification
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		//get response
		$output = curl_exec($ch);	
				
		curl_close($ch);
	}

	public function generate_otp() {
		$OTP 	=	rand(1,9);
		$OTP 	.=	rand(0,9);
		$OTP 	.=	rand(0,9);
		$OTP 	.=	rand(0,9);
		$OTP 	.=	rand(0,9);
		$OTP 	.=	rand(0,9);
		return $OTP;
	}

	public function verify() {
		$mobile		= $this->input->post('mobile');
		$otp		= $this->input->post('otp');

		// check for otp 
		$user = $this->login_model->verify($mobile, $otp);
		if($user) {
			$this->session->set_userdata($user);
			redirect('user/dashboard');
		} else {
			echo "Invalid OTP or Mobile number.";
		}
	}

}

application/controllers/User.php

class User extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('Signin_model');
	}

	public function index() {
		$this->dashboard();
	}

	public function dashboard() {
		echo "you are successfully logged in";
	}

}

application/models/Signin_model.php

class Signin_model extends CI_Model {
	public function check_mobile($mobile) {
		$this->db->where(['mobile' => $mobile]);
		$query 	= $this->db->get('user_info');
		$result = $query->num_rows();
		return $result;
	}

	public function update_otp($mobile, $data) {
		return $this->db->update('user_info', $data, ["mobile"=>$mobile]);
	}

	public function verify($mobile, $otp) {
		$data = [];
		$this->db->where([mobile => $mobile, otp => $otp]);
		$query = $this->db->get('user_info');
		$result = $query->row();
		if($result) {
			$data = [
				'login_id' 	=> $result->id,
				'login_name' 	=> $result->name,
				'login_mobile' 	=> $result->mobile,
				'login_status' 	=> TRUE,
			];
		}
		return $data;
	}
}

application/views/sign_in.php

<form method="post" action="<?php echo base_url('signin/login') ?>">
	<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
		<tr>
			<td width="230">Enter Mobile</td>
			<td width="329">
				<input type="text" name="mobile" />
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<input type="submit" name="login" value="Login" />
			</td>
		</tr>
	</table>
</form>

application/views/verify.php

<form method="post" action="<?php echo base_url('signin/verify') ?>">
	<table width="600" align="center" border="1" cellspacing="5" cellpadding="5">
		<tr>
			<td width="230">Enter OTP</td>
			<td width="329">
				<input type="hidden" name="mobile" value="<?php echo $mobile; ?>"  />
				<input type="password" name="otp" />
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<input type="submit" name="verify" value="Verify" />
			</td>
		</tr>
	</table>
</form>