Difference between revisions of "Login CodeIgniter"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
====Create Database==== | ====Create Database==== | ||
CREATE TABLE `user` ( | CREATE TABLE `user` ( | ||
− | ` | + | `user_id` int(11) NOT NULL AUTO INCREMENT, |
− | ` | + | `name` varchar(32) NOT NULL, |
`username` varchar(64) NOT NULL, | `username` varchar(64) NOT NULL, | ||
`password` varchar(255) NOT NULL, | `password` varchar(255) NOT NULL, | ||
− | `role` int(11) NOT NULL | + | `role` int(11) NOT NULL, |
+ | `user_status` int(11) NOT NULL | ||
PRIMARY KEY (`id`) | PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; | ||
Line 17: | Line 18: | ||
===Controller=== | ===Controller=== | ||
+ | ====Login==== | ||
<pre> | <pre> | ||
<?php | <?php | ||
Line 23: | Line 25: | ||
function __construct(){ | function __construct(){ | ||
parent::__construct(); | parent::__construct(); | ||
− | $this->load->model(' | + | $this->load->model('Model_login','Model_login'); |
} | } | ||
Line 35: | Line 37: | ||
} | } | ||
− | function | + | function auth(){ |
− | $ | + | $username = $this->input->post('username'); |
$password = $this->input->post('pass'); | $password = $this->input->post('pass'); | ||
− | $ | + | $validasi = $this->Model_login->query_validasi_username($username); |
− | if($ | + | if($validasi->num_rows() > 0){ |
− | $ | + | $validate_pswd=$this->Model_login->query_validasi_password($username,$password); |
− | if($ | + | if($validate_pswd->num_rows() > 0){ |
− | $x = $ | + | $x = $validate_pswd->row_array(); |
if($x['user_status']=='1'){ | if($x['user_status']=='1'){ | ||
$this->session->set_userdata('logged',TRUE); | $this->session->set_userdata('logged',TRUE); | ||
− | $this->session->set_userdata('user',$ | + | $this->session->set_userdata('user',$username); |
$id=$x['user_id']; | $id=$x['user_id']; | ||
− | if($x[' | + | if($x['role']=='1'){ //Administrator |
$name = $x['user_name']; | $name = $x['user_name']; | ||
$this->session->set_userdata('access','Administrator'); | $this->session->set_userdata('access','Administrator'); | ||
Line 53: | Line 55: | ||
$this->session->set_userdata('name',$name); | $this->session->set_userdata('name',$name); | ||
redirect('home'); | redirect('home'); | ||
− | }else if($x[' | + | }else if($x['role']=='2'){ //User |
$name = $x['user_name']; | $name = $x['user_name']; | ||
− | $this->session->set_userdata('access',' | + | $this->session->set_userdata('access','User'); |
$this->session->set_userdata('id',$id); | $this->session->set_userdata('id',$id); | ||
$this->session->set_userdata('name',$name); | $this->session->set_userdata('name',$name); | ||
Line 88: | Line 90: | ||
} | } | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | ====Home==== | ||
+ | <pre> | ||
+ | <?php | ||
+ | defined('BASEPATH') OR exit('No direct script access allowed'); | ||
+ | |||
+ | class Home extends CI_Controller { | ||
+ | function __construct(){ | ||
+ | parent::__construct(); | ||
+ | if($this->session->userdata('logged') !=TRUE){ | ||
+ | $url=base_url('login'); | ||
+ | redirect($url); | ||
+ | }; | ||
+ | } | ||
+ | |||
+ | public function index() | ||
+ | { | ||
+ | $this->load->view('view-home'); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | ===Model=== | ||
+ | ====Login==== | ||
+ | <pre> | ||
+ | <?php | ||
+ | defined('BASEPATH') OR exit('No direct script access allowed'); | ||
+ | |||
+ | class Mlogin extends CI_Model{ | ||
+ | |||
+ | function query_validasi_username($username){ | ||
+ | $result = $this->db->query("SELECT * FROM user WHERE username='$username' LIMIT 1"); | ||
+ | return $result; | ||
+ | } | ||
+ | |||
+ | function query_validasi_password($username,$password){ | ||
+ | $result = $this->db->query("SELECT * FROM user WHERE username='$username' AND user_password=SHA2('$password', 224) LIMIT 1"); | ||
+ | return $result; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ===View=== | ||
+ | ====View Login==== | ||
+ | <pre> | ||
+ | <!DOCTYPE html> | ||
+ | <html lang="en"> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
+ | <title>Form Login</title> | ||
+ | <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> | ||
+ | </head> | ||
+ | <body> | ||
+ | <br> | ||
+ | <div class="w3-container"> | ||
+ | <div class="w3-row"> | ||
+ | <div class="w3-col" style="width:30%"><p></p></div> | ||
+ | <div class="w3-col" style="width:40%"> | ||
+ | <div class="w3-card-4"> | ||
+ | <div class="w3-container w3-blue"> | ||
+ | <h2>Silahkan Login</h2> | ||
+ | </div> | ||
+ | <form class="w3-container" method="POST" action="<?php echo site_url('login/auth');?>"> | ||
+ | <p> | ||
+ | <label class="w3-text-blue"><b>Username</b></label> | ||
+ | <input class="w3-input w3-border w3-sand" name="username" type="text"> | ||
+ | </p> | ||
+ | <p> | ||
+ | <label class="w3-text-blue"><b>Password</b></label> | ||
+ | <input class="w3-input w3-border w3-sand" name="pass" type="password"> | ||
+ | </p> | ||
+ | <p> | ||
+ | <button class="w3-btn w3-blue">Masuk</button> | ||
+ | </p> | ||
+ | </form> | ||
+ | </div> | ||
+ | <div class="w3-panel w3-blue w3-display-container"> | ||
+ | <?php echo $this->session->flashdata('msg');?> | ||
+ | </div> | ||
+ | </div> | ||
+ | <div class="w3-col" style="width:30%"><p></p></div> | ||
+ | </div> | ||
+ | </div> | ||
+ | </body> | ||
+ | </html> | ||
+ | </pre> | ||
+ | |||
+ | ====View Home==== | ||
+ | <pre> | ||
+ | <!DOCTYPE html> | ||
+ | <html lang="en"> | ||
+ | <head> | ||
+ | <meta charset="UTF-8"> | ||
+ | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
+ | <title>Home</title> | ||
+ | <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> | ||
+ | </head> | ||
+ | <body> | ||
+ | <div class="w3-bar w3-border w3-light-grey w3-card-4"> | ||
+ | <a href="#" class="w3-bar-item w3-blue w3-button">Home</a> | ||
+ | <?php if($this->session->userdata('access')=='Administrator'){ ?> | ||
+ | <a href="#" class="w3-bar-item w3-button">Daftar Satu</a> | ||
+ | <a href="#" class="w3-bar-item w3-button">Daftar Dua</a> | ||
+ | <a href="#" class="w3-bar-item w3-button">Daftar Tiga</a> | ||
+ | <?php } if($this->session->userdata('access')=='User'){ ?> | ||
+ | <a href="#" class="w3-bar-item w3-button">Daftar Empat</a> | ||
+ | <?php }; ?> | ||
+ | <a href="<?php echo site_url('login/logout');?>" class="w3-bar-item w3-button w3-red w3-right">Keluar</a> | ||
+ | </div> | ||
+ | <div class="w3-container"> | ||
+ | <h1>Hai <?php echo $this->session->userdata('name'); ?></h1> | ||
+ | <h3>Ini adalah halaman untuk <?php echo $this->session->userdata('access'); ?></h3> | ||
+ | </div> | ||
+ | </body> | ||
+ | </html> | ||
</pre> | </pre> |
Revision as of 17:31, 20 November 2021
Contents
Login
Database
Create Database
CREATE TABLE `user` ( `user_id` int(11) NOT NULL AUTO INCREMENT, `name` varchar(32) NOT NULL, `username` varchar(64) NOT NULL, `password` varchar(255) NOT NULL, `role` int(11) NOT NULL, `user_status` 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
Login
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('Model_login','Model_login'); } function index(){ if($this->session->userdata('logged') !=TRUE){ $this->load->view('view-login'); }else{ $url=base_url('home'); redirect($url); }; } function auth(){ $username = $this->input->post('username'); $password = $this->input->post('pass'); $validasi = $this->Model_login->query_validasi_username($username); if($validasi->num_rows() > 0){ $validate_pswd=$this->Model_login->query_validasi_password($username,$password); if($validate_pswd->num_rows() > 0){ $x = $validate_pswd->row_array(); if($x['user_status']=='1'){ $this->session->set_userdata('logged',TRUE); $this->session->set_userdata('user',$username); $id=$x['user_id']; if($x['role']=='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['role']=='2'){ //User $name = $x['user_name']; $this->session->set_userdata('access','User'); $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','<span onclick="this.parentElement.style.display=`none`" class="w3-button w3-large w3-display-topright">×</span> <h3>Uupps!</h3> <p>Akun kamu telah di blokir. Silahkan hubungi admin.</p>'); redirect($url); } }else{ $url=base_url('login'); echo $this->session->set_flashdata('msg','<span onclick="this.parentElement.style.display=`none`" class="w3-button w3-large w3-display-topright">×</span> <h3>Uupps!</h3> <p>Password yang kamu masukan salah.</p>'); redirect($url); } }else{ $url=base_url('login'); echo $this->session->set_flashdata('msg','<span onclick="this.parentElement.style.display=`none`" class="w3-button w3-large w3-display-topright">×</span> <h3>Uupps!</h3> <p>Email yang kamu masukan salah.</p>'); redirect($url); } } function logout(){ $this->session->sess_destroy(); $url=base_url('login'); redirect($url); } }
Home
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Home extends CI_Controller { function __construct(){ parent::__construct(); if($this->session->userdata('logged') !=TRUE){ $url=base_url('login'); redirect($url); }; } public function index() { $this->load->view('view-home'); } }
Model
Login
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mlogin extends CI_Model{ function query_validasi_username($username){ $result = $this->db->query("SELECT * FROM user WHERE username='$username' LIMIT 1"); return $result; } function query_validasi_password($username,$password){ $result = $this->db->query("SELECT * FROM user WHERE username='$username' AND user_password=SHA2('$password', 224) LIMIT 1"); return $result; } }
View
View Login
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form Login</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> </head> <body> <br> <div class="w3-container"> <div class="w3-row"> <div class="w3-col" style="width:30%"><p></p></div> <div class="w3-col" style="width:40%"> <div class="w3-card-4"> <div class="w3-container w3-blue"> <h2>Silahkan Login</h2> </div> <form class="w3-container" method="POST" action="<?php echo site_url('login/auth');?>"> <p> <label class="w3-text-blue"><b>Username</b></label> <input class="w3-input w3-border w3-sand" name="username" type="text"> </p> <p> <label class="w3-text-blue"><b>Password</b></label> <input class="w3-input w3-border w3-sand" name="pass" type="password"> </p> <p> <button class="w3-btn w3-blue">Masuk</button> </p> </form> </div> <div class="w3-panel w3-blue w3-display-container"> <?php echo $this->session->flashdata('msg');?> </div> </div> <div class="w3-col" style="width:30%"><p></p></div> </div> </div> </body> </html>
View Home
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home</title> <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> </head> <body> <div class="w3-bar w3-border w3-light-grey w3-card-4"> <a href="#" class="w3-bar-item w3-blue w3-button">Home</a> <?php if($this->session->userdata('access')=='Administrator'){ ?> <a href="#" class="w3-bar-item w3-button">Daftar Satu</a> <a href="#" class="w3-bar-item w3-button">Daftar Dua</a> <a href="#" class="w3-bar-item w3-button">Daftar Tiga</a> <?php } if($this->session->userdata('access')=='User'){ ?> <a href="#" class="w3-bar-item w3-button">Daftar Empat</a> <?php }; ?> <a href="<?php echo site_url('login/logout');?>" class="w3-bar-item w3-button w3-red w3-right">Keluar</a> </div> <div class="w3-container"> <h1>Hai <?php echo $this->session->userdata('name'); ?></h1> <h3>Ini adalah halaman untuk <?php echo $this->session->userdata('access'); ?></h3> </div> </body> </html>