<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[Catatan Harian: Membuat Halaman Login }]]></title>
	<link>https://sharka.site/3blog/pages/view/62/membuat-halaman-login</link>
	<atom:link href="https://sharka.site/3blog/pages/view/62/membuat-halaman-login" rel="self" type="application/rss+xml" />
	<description><![CDATA[}]]></description>
		<item>
	<guid isPermaLink="true">https://sharka.site/3blog/pages/view/62/membuat-halaman-login</guid>
	<pubDate>Wed, 22 Oct 2025 17:15:47 +0800</pubDate>
	<link>https://sharka.site/3blog/pages/view/62/membuat-halaman-login</link>
	<title><![CDATA[Membuat Halaman Login ]]></title>
	<description><![CDATA[<div style="color:#222;font-family:'Poppins',sans-serif;line-height:1.7;margin:0 auto;max-width:950px;"><div style="background-color:#f1f5ff;border-radius:12px;box-shadow:0 4px 12px rgba(2,62,138,0.1);color:#023e8a;padding:25px;text-align:center;"><h2 style="font-size:22px;margin:0;">🔐 <strong>Tutorial Membuat Halaman Login di CodeIgniter 4</strong></h2><p style="color:#014f86;font-size:14px;margin:10px 0 0;">Lengkap dengan Model, Controller, Routes, View, dan Filter</p></div><div style="margin-top:20px;"><p>Pada tutorial ini, kita akan membuat sistem <strong>Login</strong> sederhana di <strong>CodeIgniter 4</strong> yang menggunakan tabel pengguna berisi <code>username</code>, <code>password</code>, dan <code>level</code> (admin atau user). Setelah berhasil login, pengguna akan diarahkan ke halaman berbeda sesuai levelnya.</p></div><h3 style="color:#023e8a;margin-top:25px;">📊 1. Struktur Database</h3><p>Buat tabel <code>users</code> pada database Anda, misalnya dengan SQL berikut:</p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  password VARCHAR(255) NOT NULL,
  level ENUM('admin','user') DEFAULT 'user'
);</pre></div><p>Contoh data awal:</p><div style="background-color:#f8fafc;border-radius:8px;border:1px solid #e8eefc;padding:10px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">INSERT INTO users (username, password, level)
VALUES 
('admin', PASSWORD('12345'), 'admin'),
('user1', PASSWORD('12345'), 'user');</pre></div><div style="background-color:#fff3bf;border-left:4px solid #f59f00;border-radius:8px;margin-top:10px;padding:12px;"><strong>Catatan:</strong> Saat di PHP/CI4 gunakan <code>password_hash()</code> untuk menyimpan password, bukan fungsi <code>PASSWORD()</code> MySQL.</div><h3 style="color:#023e8a;margin-top:25px;">🧩 2. Membuat Model UserModel.php</h3><p>Buat file: <code>app/Models/UserModel.php</code></p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['username', 'password', 'level'];
    public function getUser($username)
    {
        return $this-&gt;where('username', $username)-&gt;first();
    }
}</pre></div><h3 style="color:#023e8a;margin-top:25px;">🧭 3. Membuat Controller Auth.php</h3><p>Buat file: <code>app/Controllers/Auth.php</code></p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Controllers;
use App\Models\UserModel;
class Auth extends BaseController
{
    public function login()
    {
        return view('auth/login');
    }
    public function doLogin()
    {
        $session = session();
        $model = new UserModel();
        $username = $this-&gt;request-&gt;getPost('username');
        $password = $this-&gt;request-&gt;getPost('password');
        $data = $model-&gt;getUser($username);
        if ($data) {
            $pass = $data['password'];
            if (password_verify($password, $pass)) {
                $ses_data = [
                    'id' =&gt; $data['id'],
                    'username' =&gt; $data['username'],
                    'level' =&gt; $data['level'],
                    'logged_in' =&gt; TRUE
                ];
                $session-&gt;set($ses_data);
                if ($data['level'] == 'admin') {
                    return redirect()-&gt;to('/admin/dashboard');
                } else {
                    return redirect()-&gt;to('/user/dashboard');
                }
            } else {
                $session-&gt;setFlashdata('msg', 'Password salah!');
                return redirect()-&gt;back();
            }
        } else {
            $session-&gt;setFlashdata('msg', 'Username tidak ditemukan!');
            return redirect()-&gt;back();
        }
    }
    public function logout()
    {
        session()-&gt;destroy();
        return redirect()-&gt;to('/login');
    }
}</pre></div><h3 style="color:#023e8a;margin-top:25px;">🖥️ 4. Membuat View Login</h3><p>Buat file: <code>app/Views/auth/login.php</code></p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;!DOCTYPE html&gt;
&lt;html lang="id"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;title&gt;Login - CodeIgniter 4&lt;/title&gt;
  &lt;style&gt;
    body {font-family: 'Poppins', sans-serif;background:#f5f6fa;}
    .login-box {width:320px;margin:100px auto;background:white;padding:30px;border-radius:12px;box-shadow:0 5px 15px rgba(0,0,0,0.1);}
    input {width:100%;padding:10px;margin:8px 0;border:1px solid #ccc;border-radius:8px;}
    button {width:100%;padding:10px;background:#023e8a;color:#fff;border:none;border-radius:8px;cursor:pointer;}
    button:hover {background:#0353a4;}
    .msg {color:red;text-align:center;font-size:13px;margin-bottom:10px;}
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class="login-box"&gt;
    &lt;h2 style="text-align:center;color:#023e8a;margin-bottom:20px;"&gt;Form Login&lt;/h2&gt;
    &lt;?php if (session()-&gt;getFlashdata('msg')): ?&gt;
      &lt;div class="msg"&gt;&lt;?= session()-&gt;getFlashdata('msg'); ?&gt;&lt;/div&gt;
    &lt;?php endif; ?&gt;
    &lt;form action="/doLogin" method="post"&gt;
      &lt;input type="text" name="username" placeholder="Username" required&gt;
      &lt;input type="password" name="password" placeholder="Password" required&gt;
      &lt;button type="submit"&gt;Login&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div><h3 style="color:#023e8a;margin-top:25px;">🛣️ 5. Menambahkan Routes</h3><p>Edit file: <code>app/Config/Routes.php</code></p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">$routes-&gt;get('/login', 'Auth::login');
$routes-&gt;post('/doLogin', 'Auth::doLogin');
$routes-&gt;get('/logout', 'Auth::logout');
$routes-&gt;get('/admin/dashboard', 'Admin::index', ['filter' =&gt; 'auth']);
$routes-&gt;get('/user/dashboard', 'User::index', ['filter' =&gt; 'auth']);</pre></div><h3 style="color:#023e8a;margin-top:25px;">🧱 6. Membuat Filter Autentikasi</h3><p>Buat file: <code>app/Filters/AuthFilter.php</code></p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        if (! session()-&gt;get('logged_in')) {
            return redirect()-&gt;to('/login');
        }
    }
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        // kosong
    }
}</pre></div><h3 style="color:#023e8a;margin-top:25px;">⚙️ 7. Daftarkan Filter ke Config/Filters.php</h3><p>Edit file: <code>app/Config/Filters.php</code> dan tambahkan di bagian <strong>aliases</strong>:</p><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e8eefc;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">public array $aliases = [
    'csrf' =&gt; CSRF::class,
    'toolbar' =&gt; DebugToolbar::class,
    'honeypot' =&gt; Honeypot::class,
    'auth' =&gt; \App\Filters\AuthFilter::class,
];</pre></div><h3 style="color:#023e8a;margin-top:25px;">🎉 8. Hasil Akhir</h3><ul><li>Login berhasil akan menyimpan sesi dan mengarahkan ke halaman dashboard sesuai level.</li><li>Jika belum login, pengguna akan otomatis diarahkan ke halaman login.</li><li>Gunakan <code>session()-&gt;get('level')</code> di halaman lain untuk menampilkan peran pengguna.</li></ul><div style="border-radius:10px;border:1px solid #e6eefc;margin-top:20px;padding:14px;"><p><strong style="color:#023e8a;">Kesimpulan:</strong></p><p style="font-size:14px;margin:8px 0 0;">Dengan langkah-langkah di atas, Anda telah membuat sistem login sederhana di CodeIgniter 4 yang aman menggunakan <code>password_hash()</code>, sesi login, dan filter autentikasi. Anda bisa mengembangkannya dengan fitur tambahan seperti <i>remember me</i>, manajemen pengguna, atau login multi-level lanjutan.</p><p style="font-size:14px;margin:8px 0 0;">&nbsp;</p><p style="font-size:14px;margin:8px 0 0;">Contoh :&nbsp;</p></div></div><p>====&nbsp;</p><div style="color:#222;font-family:'Poppins',sans-serif;line-height:1.7;margin:0 auto;max-width:900px;"><div style="background-color:#e7f2ff;border-radius:12px;box-shadow:0 4px 16px rgba(2,62,138,0.08);color:#023e8a;padding:20px;text-align:center;"><h2 style="margin:0;">🔐 <strong>Tutorial Membuat Login dengan CodeIgniter 4</strong></h2><p style="color:#014f86;font-size:14px;margin-top:8px;">Lengkap dengan Model, View, Controller, Routes, dan Filter</p></div><h3 style="color:#023e8a;margin-top:22px;">📘 Pendahuluan</h3><p><strong>CodeIgniter 4 (CI4)</strong> mendukung konsep MVC dan sistem <i>routing</i> yang modern. Dalam tutorial ini, kita akan membuat sistem login sederhana dengan tabel database berisi:</p><div style="background-color:#fff;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;">users
├── id (INT, AUTO_INCREMENT)
├── username (VARCHAR)
├── password (VARCHAR)
└── level (ENUM: 'admin', 'user')</pre></div><h3 style="color:#023e8a;margin-top:22px;">🧠 Struktur Folder CI4</h3><div style="background-color:#f8f9ff;border-radius:8px;border:1px solid #e2e6f3;padding:10px;"><pre style="font-size:13px;margin:0;">/app
 ├── Controllers
 │    ├── Login.php
 │    ├── Dashboard.php
 │
 ├── Models
 │    └── UserModel.php
 │
 ├── Views
 │    ├── login.php
 │    ├── dashboard_admin.php
 │    └── dashboard_user.php
 │
 ├── Filters
 │    └── AuthFilter.php
</pre></div><h3 style="color:#023e8a;margin-top:22px;">1️⃣ Membuat Model: <code>UserModel.php</code></h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Models;

use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';
    protected $allowedFields = ['username', 'password', 'level'];

    public function getUser($username)
    {
        return $this-&gt;where('username', $username)-&gt;first();
    }
}
</pre></div><h3 style="color:#023e8a;margin-top:22px;">2️⃣ Membuat Controller: <code>Login.php</code></h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Controllers;

use App\Models\UserModel;

class Login extends BaseController
{
    public function index()
    {
        return view('login');
    }

    public function auth()
    {
        $session = session();
        $model = new UserModel();
        $username = $this-&gt;request-&gt;getPost('username');
        $password = $this-&gt;request-&gt;getPost('password');
        $data = $model-&gt;getUser($username);

        if ($data) {
            if (password_verify($password, $data['password'])) {
                $sessionData = [
                    'id' =&gt; $data['id'],
                    'username' =&gt; $data['username'],
                    'level' =&gt; $data['level'],
                    'logged_in' =&gt; true
                ];
                $session-&gt;set($sessionData);

                if ($data['level'] === 'admin') {
                    return redirect()-&gt;to('/dashboard/admin');
                } else {
                    return redirect()-&gt;to('/dashboard/user');
                }
            } else {
                $session-&gt;setFlashdata('msg', 'Password salah.');
                return redirect()-&gt;to('/login');
            }
        } else {
            $session-&gt;setFlashdata('msg', 'Username tidak ditemukan.');
            return redirect()-&gt;to('/login');
        }
    }

    public function logout()
    {
        session()-&gt;destroy();
        return redirect()-&gt;to('/login');
    }
}
</pre></div><h3 style="color:#023e8a;margin-top:22px;">3️⃣ Membuat View: <code>login.php</code></h3><div style="background-color:#fff;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Login&lt;/title&gt;
  &lt;style&gt;
    body{font-family:Poppins,sans-serif;background:#f1f3f6;display:flex;align-items:center;justify-content:center;height:100vh;}
    .login-box{background:#fff;padding:30px;border-radius:12px;width:320px;box-shadow:0 6px 12px rgba(0,0,0,0.08);}
    input{width:100%;padding:10px;margin:6px 0;border:1px solid #ccc;border-radius:6px;}
    button{width:100%;padding:10px;background:#0077b6;color:#fff;border:none;border-radius:6px;cursor:pointer;}
    button:hover{background:#023e8a;}
  &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div class="login-box"&gt;
    &lt;h3&gt;🔐 Login&lt;/h3&gt;
    &lt;?php if(session()-&gt;getFlashdata('msg')): ?&gt;
      &lt;p style="color:red;"&gt;&lt;?= session()-&gt;getFlashdata('msg'); ?&gt;&lt;/p&gt;
    &lt;?php endif; ?&gt;
    &lt;form method="post" action="/login/auth"&gt;
      &lt;input type="text" name="username" placeholder="Username" required&gt;
      &lt;input type="password" name="password" placeholder="Password" required&gt;
      &lt;button type="submit"&gt;Login&lt;/button&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre></div><h3 style="color:#023e8a;margin-top:22px;">4️⃣ Membuat Controller Dashboard</h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Controllers;

class Dashboard extends BaseController
{
    public function admin()
    {
        echo view('dashboard_admin');
    }

    public function user()
    {
        echo view('dashboard_user');
    }
}
</pre></div><h3 style="color:#023e8a;margin-top:22px;">5️⃣ Membuat Filter: <code>AuthFilter.php</code></h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;?php
namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class AuthFilter implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        if (!session()-&gt;get('logged_in')) {
            return redirect()-&gt;to('/login');
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        // Kosong
    }
}
</pre></div><h3 style="color:#023e8a;margin-top:22px;">6️⃣ Daftarkan Filter ke <code>Config/Filters.php</code></h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">public $aliases = [
    'csrf'    =&gt; \CodeIgniter\Filters\CSRF::class,
    'toolbar' =&gt; \CodeIgniter\Filters\DebugToolbar::class,
    'auth'    =&gt; \App\Filters\AuthFilter::class,
];
</pre></div><h3 style="color:#023e8a;margin-top:22px;">7️⃣ Tambahkan Route</h3><div style="background-color:#0f172a0a;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">$routes-&gt;get('/login', 'Login::index');
$routes-&gt;post('/login/auth', 'Login::auth');
$routes-&gt;get('/logout', 'Login::logout');

$routes-&gt;group('dashboard', ['filter' =&gt; 'auth'], function($routes){
    $routes-&gt;get('admin', 'Dashboard::admin');
    $routes-&gt;get('user', 'Dashboard::user');
});
</pre></div><h3 style="color:#023e8a;margin-top:22px;">8️⃣ Menambahkan Extend &amp; Section (untuk View Dashboard)</h3><div style="background-color:#fff;border-radius:8px;border:1px solid #e3e8f5;padding:12px;"><pre style="font-size:13px;margin:0;white-space:pre-wrap;">&lt;!-- layout_main.php --&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Dashboard&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
  &lt;header&gt;&lt;h3&gt;Halo, &lt;?= session('username'); ?&gt;&lt;/h3&gt;&lt;/header&gt;
  &lt;main&gt;
    &lt;?= $this-&gt;renderSection('content'); ?&gt;
  &lt;/main&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;!-- dashboard_admin.php --&gt;
&lt;?= $this-&gt;extend('layout_main'); ?&gt;
&lt;?= $this-&gt;section('content'); ?&gt;
  &lt;h2&gt;Selamat datang, Admin!&lt;/h2&gt;
&lt;?= $this-&gt;endSection(); ?&gt;
</pre></div><div style="border-left:4px solid #0096c7;margin-top:24px;padding-left:12px;"><p><strong>💬 Kesimpulan:</strong></p><p style="font-size:14px;margin:6px 0 0;">Dengan langkah-langkah di atas, Anda sudah memiliki sistem login dasar di CI4 lengkap dengan autentikasi, session, dan proteksi halaman dengan filter.</p></div></div>]]></description>
	<dc:creator>Muh.Taqiuddin.S</dc:creator>		</item>
</channel>
</rss>
