Difference between revisions of "Login CodeIgniter"
Jump to navigation
Jump to search
(Created page with "==Login== ===Database=== ====Create Database==== CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO INCREMENT, `fullname` varchar(32) NOT NULL, `username` varchar(64) NOT...") |
|||
Line 22: | Line 22: | ||
function __construct(){ | function __construct(){ | ||
parent::__construct(); | parent::__construct(); | ||
− | + | $this->load->model('Mlogin','Mlogin'); | |
} | } | ||
− | + | function index(){ | |
if($this->session->userdata('logged') !=TRUE){ | if($this->session->userdata('logged') !=TRUE){ | ||
$this->load->view('view-login'); | $this->load->view('view-login'); |
Revision as of 16:40, 20 November 2021
Contents
Login
Database
Create Database
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO INCREMENT, `fullname` varchar(32) NOT NULL, `username` varchar(64) NOT NULL, `password` varchar(255) NOT NULL, `role` int(11) NOT NULL PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
Insert Data
INSERT INTO table_user VALUES (,'User','user',SHA2('user123', 224),'2'), (,'Admin','admin',SHA2('admin123', 224),'1');
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { function __construct(){ parent::__construct();
$this->load->model('Mlogin','Mlogin');
}
function index(){
if($this->session->userdata('logged') !=TRUE){
$this->load->view('view-login');
}else{
$url=base_url('home');
redirect($url);
};
}
function autentikasi(){
$email = $this->input->post('email');
$password = $this->input->post('pass');
$validasi_email = $this->Mlogin->query_validasi_email($email);
if($validasi_email->num_rows() > 0){
$validate_ps=$this->Mlogin->query_validasi_password($email,$password);
if($validate_ps->num_rows() > 0){
$x = $validate_ps->row_array();
if($x['user_status']=='1'){
$this->session->set_userdata('logged',TRUE);
$this->session->set_userdata('user',$email);
$id=$x['user_id'];
if($x['user_akses']=='1'){ //Administrator
$name = $x['user_name'];
$this->session->set_userdata('access','Administrator');
$this->session->set_userdata('id',$id);
$this->session->set_userdata('name',$name);
redirect('home');
}else if($x['user_akses']=='2'){ //User
$name = $x['user_name'];
$this->session->set_userdata('access','Dosen');
$this->session->set_userdata('id',$id);
$this->session->set_userdata('name',$name);
redirect('home');
}else{
$url=base_url('login');
echo $this->session->set_flashdata('msg','
Uupps!
Akun kamu telah di blokir. Silahkan hubungi admin.
');
redirect($url);
}
}else{
$url=base_url('login');
echo $this->session->set_flashdata('msg','
Uupps!
Password yang kamu masukan salah.
');
redirect($url);
}
}else{
$url=base_url('login');
echo $this->session->set_flashdata('msg','
Uupps!
Email yang kamu masukan salah.
');
redirect($url); } } function logout(){ $this->session->sess_destroy(); $url=base_url('login'); redirect($url); } }