Codigniter Login and Registration with Jquery

Simple Login and Registration process with Codigniter MVC Architecture.

Create Login.php Controller

 

<?php
/**
*
*
* Registration Details
*
* PHP version 5.5
*

* */
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('minify');
}
public function index() {

$user = $this->session->userdata('user');
if (empty($user)) {
$this->load->view('doctor/Login');
/* $this->load->view('doctor/Login', array(
'header' => true
//'data' => $data,
//'sidebar' => true
));*/
}
else
{
/* $this->load->view('doctor/patient_registration', array(
'header' => false,
'data' => ''
)); */
if($user['user_type']=='frontdesk'){
$this->load->view('doctor/patient_frontdesk', array(
'header' => false,
'data' => ''
));
}
else if($user['user_type']=='staff'){
$this->load->view('doctor/patient_staff', array(
'header' => false,
'data' => ''
));
}
else if($user['user_type']=='doctor'){
$this->load->view('doctor/doctor', array(
'header' => false,
'data' => ''
));
}
else
{
$this->load->view('doctor/patient_registration', array(
'header' => false,
'data' => ''
));
}
}
}

//function for Login Page
function login() {
$emailId = trim($this->input->post('user_name'));
$password = trim($this->input->post('password'));
$remember = $this->input->post('remember');
//$user_type = $this->input->post('user_type');
$user_type='';
$emailId = $this->adminmodel->admin_medley_encrypt($emailId);
$result = $this->user->checkLogin($emailId, $password, $user_type);
if ($result) {
if ($result['st'] == 1) {
// echo "hello...";
if ($remember) {
$month = time() + (60 * 60 * 24 * 30);
$this->input->set_cookie('remember', $emailId, $month);
$this->input->set_cookie('emailId', $emailId, $month);
$this->input->set_cookie('password', $result['password'], $month);
} else if (!$remember) {
$past = time() - 100;
if (isset($_COOKIE['remember'])) {
$this->input->set_cookie('remember', '', $past);
}
if (isset($_COOKIE['emailId'])) {
$this->input->set_cookie('emailId', '', $past);
}
if (isset($_COOKIE['password'])) {
$this->input->set_cookie('password', '', $past);
}
}
$result['id'] = $result['id'];
$result['name'] = $this->adminmodel->admin_medley_decrypt($result['name']);
$result['username'] = $this->adminmodel->admin_medley_decrypt($result['username']);
$result['mobile_no'] = $this->adminmodel->admin_medley_decrypt($result['mobile_no']);
$result['email'] = $this->adminmodel->admin_medley_decrypt($result['email']);
/* Clinic info */
$result['clinic_name'] = $result['clinic_name'];
$result['clinic_user_tbl_id'] = $result['clinic_user_tbl_id'];
$result['clinic_tbl_id'] = $result['clinic_tbl_id'];
/* end */
$result['user_type'] = $result['user_type'];
if ($result['user_type']==='doctor' || $result['user_type'] ==='staff' || $result['user_type'] ==='frontdesk') {
$this->session->set_userdata('user', $result);
}

}
echo json_encode($result);
}
}

//Start of Logout
public function logout() {
$user = $this->session->userdata('user');
$this->session->unset_userdata('user');
$this->session->sess_destroy();
redirect('/');
}
/* ends */

/* End */
}

 

Step 2 create Model according to Login Controller;

\application\models\login.php

<?php

/**
*
*
* Registration Details
*
* PHP version 3.0
*

* */
class user extends CI_Model {

var $user_session = array();

function __construct() {
}

//function for check Login details
function checkLogin($emailId, $password, $user_type) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
$browser_name = $_SERVER['HTTP_USER_AGENT'];
$today = $this->config->item('today_date');
$this->db->select('id,name,username,password,mobile_no,email,user_type,created_datetime,modified_datetime');
//$checkLogin = $this->db->get_where('users', array('user_type' => $user_type, 'email' => $emailId, 'status' => '1'));
$checkLogin = $this->db->get_where('users', array('email' => $emailId, 'status' => '1'));
if ($checkLogin->num_rows() <= 0) {
return array('st' => 0, 'msg' => 'Invalid Email or Password');
}
$result = $checkLogin->row_array();
$salt = explode(':', $result['password']);
$new_password = $this->adminmodel->password_decrypt($password, $salt[1]);
if ($result['password'] != $new_password) {
return array('st' => 0, 'msg' => 'Invalid Email or Password');
} else if ($checkLogin->num_rows() > 0) {
$clinic_user_table_id='';
if ($result['user_type']==='doctor')
{

$this->db->select('user_tbl_id,clinic_name,address,clinic_tbl.sno "clinic_tbl_id"');
$this->db->from('clinic_tbl');
$this->db->join('doctor_clinic_mapping_tbl', 'doctor_clinic_mapping_tbl.clinic_id = clinic_tbl.sno');
$this->db->where('doctor_clinic_mapping_tbl.doctor_id =',$result['id']);
$getclinic = $this->db->get();
$getclinic_result = $getclinic->result();
if ($getclinic->num_rows() > 0) {
$clinic_user_table_id=$getclinic_result[0]->user_tbl_id;
$result['clinic_name'] = $this->adminmodel->admin_medley_decrypt($getclinic_result[0]->clinic_name);
$result['address'] = $this->adminmodel->admin_medley_decrypt($getclinic_result[0]->address);
$result['clinic_user_tbl_id'] = $getclinic_result[0]->user_tbl_id;
$result['clinic_tbl_id'] = $getclinic_result[0]->clinic_tbl_id;
}

}
else
{

$this->db->select('user_tbl_id,clinic_name,address,clinic_tbl.sno "clinic_tbl_id"');
$this->db->from('clinic_tbl');
$this->db->join('staff_clinic_mapping_tbl', 'staff_clinic_mapping_tbl.clinic_id = clinic_tbl.sno');
$this->db->where('staff_clinic_mapping_tbl.staff_id =',$result['id']);
$getclinic = $this->db->get();
$getclinic_result = $getclinic->result();

if ($getclinic->num_rows() > 0) {
$clinic_user_table_id=$getclinic_result[0]->user_tbl_id;
$result['clinic_name'] = $this->adminmodel->admin_medley_decrypt($getclinic_result[0]->clinic_name);
$result['address'] = $this->adminmodel->admin_medley_decrypt($getclinic_result[0]->address);
$result['clinic_user_tbl_id'] = $getclinic_result[0]->user_tbl_id;
$result['clinic_tbl_id'] = $getclinic_result[0]->clinic_tbl_id;
}
}
/* $query_clinic_details = $this->db->query("call getClinicData('" . $clinic_user_table_id . "')");
$getClinicInfo = $query_clinic_details->result();

foreach ($getClinicInfo as $key => $val)
{
$result['clinic_name'] = $this->adminmodel->admin_medley_decrypt($getClinicInfo[$key]->clinic_name);
} */

$qry = "INSERT INTO `user_login_history_tbl`(`login_user_id`, `login_system_ip`,`login_browser`,`login_datetime`,
`created_datetime`) VALUES ('" . $result['id'] . "','" . $ipaddress . "','" . $browser_name . "','" . $today . "','" . $today . "')";
$saveLoginDetails = $this->db->query($qry);
$unique_id = $this->db->insert_id();
$result['unique_id'] = $unique_id;
}
$result['st'] = 1;
return $result;
}

}

?>

Step 3 create view:

\application\views\prescription\doctor\login.php

 

<div class="col-lg-12 col-md-12 col-sm-12 col-12 login_from align-middle mx-auto">
<!--<form id="login-form_1" action="" method="post" role="form">-->
<div id="validation_login_error_details" ></div>
<?php echo form_open('login/login', array('id' => 'doctorLoginDetailsId')); ?>

<div class="col-lg-12 col-md-12 col-sm-12 col-12">
<div class="col-lg-8 col-md-8 col-sm-8 col-12 mx-auto mb-4">
<div class="left-inner-addon ">
<img src="<?php echo base_url(); ?>images/icon_userid.png" alt="">
<input title="Enter Email" type="text" class="form-control required" placeholder="Email" name="user_name" id="id_email" value="<?php echo (isset($_COOKIE['username'])) ? $_COOKIE['username'] : ''; ?>">
</div>
</div>
</div>

<div class="col-lg-12 col-md-12 col-sm-12 col-12">
<div class="col-lg-8 col-md-8 col-sm-8 col-12 mx-auto mb-3">
<div class="left-inner-addon ">
<img src="<?php echo base_url(); ?>images/icon_password.png" alt="">
<a onclick="showpass()"><i class="fa fa-eye"></i></a>
<input title="Enter Password" type="password" class="form-control required" placeholder="Password" name="password" id="id_password" value="<?php echo (isset($_COOKIE['password'])) ? $_COOKIE['password'] : ''; ?>" >

</div>
</div>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-12">
<div class="col-lg-8 col-md-8 col-sm-8 col-12 mx-auto mb-3">
<a class="float-right text-underline forgot">Forgot Password?</a>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-12 mx-auto pt-4">
<button type="submit" class="btn btn-theme col-md-12">Login</button>
</div>
</form>

</div>

 

<!--body-->
<script>
$('#doctorLoginDetailsId').validate({
errorPlacement: function () {
return false;
},
showErrors: function (errorMap, errorList) {
$.each(this.validElements(), function (index, element) {
var $element = $(element);
$element.data("title", "")
.removeClass("error")
.tooltip("destroy");
});
$.each(errorList, function (index, error) {
var $element = $(error.element);
$element.tooltip("destroy")
.data("title", error.message)
.addClass("error")
.tooltip();
});
},
submitHandler: function () {
var formData = new FormData($('form')[0]);
var SITEURL = "<?php echo base_url(); ?>";
$.ajax({
//url: form.action,
url:SITEURL + 'login/login',
type: 'POST',
data: formData,
contentType: false,
processData: false,
cache: false,
beforeSend: function (xhr) {
$("#send_login_details_id").attr('disabled', true).html('Loading...');
},
success: function (response) {
var data = JSON.parse(response);
console.log(data);

if (data.st === 1) {

if (data.user_type ==='doctor') {
window.location.href = SITEURL + 'prescription';
}
else if (data.user_type ==='staff') {
window.location.href = SITEURL + 'patientinfo';
}
else if (data.user_type ==='frontdesk') {
window.location.href = SITEURL + 'patientinfo';
}
else {
$('#validation_login_error_details').html('User type no Matched!').addClass('alert alert-danger');
setTimeout(function () {
$('#validation_login_error_details').html('').removeClass('alert alert-danger');
//window.location.href = SITEURL + 'login';
}, 3000);
}
}
if (data.st == 0) {
$('#validation_login_error_details').html(data.msg).addClass('alert alert-danger');
setTimeout(function () {
$('#validation_login_error_details').html('').removeClass('alert alert-danger');
//window.location.href = SITEURL + 'login';
}, 3000);
$("#send_login_details_id").attr('disabled', false).html('Submit');
}
}
});
}
});
/* End */
</script>
Js library File That you need

<script src="<?php echo base_url(); ?>themes/prescription/frontend/lib/jquery/jquery.min.js"></script>

<script src="<?php echo base_url(); ?>themes/prescription/frontend/lib/bootstrap/js/bootstrap.min.js"></script>

<script src="<?php echo base_url(); ?>themes/prescription/plugins/validation/jquery.validate.js"></script>

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons
Social Media Auto Publish Powered By : XYZScripts.com