[Pemprograman Berbasis KK] Tugas 4
Nitama Nurlingga Yotifa
05111740000059
05111740000059
Pengenalan Framework Code Igniter [3]
Mengatur Hak Akses User
- Membuat Tabel untuk User
- Membuat Model User pada application/models/User_model.php
- Membuat Controller Login pada controllers/admin/Login.php
- Membuat View untuk Halaman Login pada application/view/admin/login_page.php
- Uji Coba Fitur Login
Buat tabel dengan nama users dan buat skema tabelnya seperti pada gambar dibawah.
Kemudian insert data admin.
1: <?php
2: class User_model extends CI_Model
3: {
4: private $_table = "users";
5: public function doLogin(){
6: $post = $this->input->post();
7: // cari user berdasarkan email dan username
8: $this->db->where('email', $post["email"])
9: ->or_where('username', $post["email"]);
10: $user = $this->db->get($this->_table)->row();
11: // jika user terdaftar
12: if($user){
13: // periksa password-nya
14: $isPasswordTrue = password_verify($post["password"], $user->password);
15: // periksa role-nya
16: $isAdmin = $user->role == "admin";
17: // jika password benar dan dia admin
18: if($isPasswordTrue && $isAdmin){
19: // login sukses yay!
20: $this->session->set_userdata(['user_logged' => $user]);
21: $this->_updateLastLogin($user->user_id);
22: return true;
23: }
24: }
25: // login gagal
26: return false;
27: }
28: public function isNotLogin(){
29: return $this->session->userdata('user_logged') === null;
30: }
31: private function _updateLastLogin($user_id){
32: $sql = "UPDATE {$this->_table} SET last_login=now() WHERE user_id={$user_id}";
33: $this->db->query($sql);
34: }
35: }
1: <?php
2: class Login extends CI_Controller
3: {
4: public function __construct()
5: {
6: parent::__construct();
7: $this->load->model("user_model");
8: $this->load->library('form_validation');
9: }
10: public function index()
11: {
12: // jika form login disubmit
13: if($this->input->post()){
14: if($this->user_model->doLogin()) redirect(site_url('admin'));
15: }
16: // tampilkan halaman login
17: $this->load->view("admin/login_page.php");
18: }
19: public function logout()
20: {
21: // hancurkan semua sesi
22: $this->session->sess_destroy();
23: redirect(site_url('admin/login'));
24: }
25: }
1: <!DOCTYPE html>
2: <html lang="en">
3: <head>
4: <meta charset="UTF-8">
5: <meta name="viewport" content="width=device-width, initial-scale=1.0">
6: <meta http-equiv="X-UA-Compatible" content="ie=edge">
7: <title>Login Admin</title>
8: <!-- Bootstrap core CSS-->
9: <link href="<?php echo base_url('css/styles.css') ?>" rel="stylesheet">
10: </head>
11: <body>
12: <div class="container">
13: <div class="row">
14: <div class="col-12 col-md-6 text-center mt-5 mx-auto p-4">
15: <h1 class="h2">Login Admin</h1>
16: <p class="lead">Silahkan masuk ke Panel Admin</p>
17: </div>
18: </div>
19: <div class="row">
20: <div class="col-12 col-md-5 mx-auto mt-5">
21: <form action="<?= site_url('admin/login') ?>" method="POST">
22: <div class="form-group">
23: <label for="email">Email</label>
24: <input type="text" class="form-control" name="email" placeholder="Pakai username juga bisa.." required />
25: </div>
26: <div class="form-group">
27: <label for="password">Password</label>
28: <input type="password" class="form-control" name="password" placeholder="Password.." required />
29: </div>
30: <div class="form-group">
31: <div class="d-flex justify-content-between">
32: <div class="custom-control custom-checkbox">
33: <input type="checkbox" class="custom-control-input" name="rememberme" id="rememberme" />
34: <label class="custom-control-label" for="rememberme"> Ingat Saya</label>
35: </div>
36: <a href="<?= site_url('reset_password') ?>">Lupa Password?</a>
37: </div>
38: </div>
39: <div class="form-group">
40: <input type="submit" class="btn btn-success w-100" value="Login" />
41: </div>
42: </form>
43: </div>
44: </div>
45: </div>
46: </body>
47: </html>
Komentar
Posting Komentar