Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public function login(){ if(!empty($_POST)){ // Form Validation--> $validation = \Config\Services::validation(); $validation->setRules([ 'username' => ['label' => 'Username', 'rules' => 'required'], 'password' => ['label' => 'Password', 'rules' => 'required'] ]); // Form Validation Close--> if(!$validation->withRequest($this->request)->run()) { $validationErrors = $validation->listErrors(); // Redirect back with validation errors return redirect()->back()->with('error', $validationErrors); }else{ $username = $this->request->getPost('username'); $password = $this->request->getPost('password'); $user = $this->db->table('superadmin')->where('username', $username)->get()->getRow(); if ($user) { // Verify the password if (verify_password($password, $user->password)) { // Set session or other login logic session()->set(['username' => $user->username]); return redirect()->to('/superadmin/dashboard')->with('success', 'Login successful!'); } else { return redirect()->back()->with('error', 'Invalid credentials!'); } }else { return redirect()->back()->with('error', 'User not found!'); } } } else{ echo view('superadmin/includes/header'); echo view('superadmin/login'); echo view('superadmin/includes/footer'); } } |
Password:
1. Created a Helper Function
created helper functions for password hashing and verification to reuse them across your application.
Create a file in app/Helpers, e.g., app/Helpers/password_helper.php
hash_password($password)
verify_password($input_password, $stored_hash)
Load this helper in your application:
helper('password');
2. Hash Password During Registration
$password = $this->request->getPost('password');
$hashed_password = hash_password($password);
3. Verify Password During Login
$password = $this->request->getPost('password');
verify_password($input_password, $user->password)
If password verify will return 1, else blank.
Redirect (Back to request page):
return redirect()->back()->with('error', 'Invalid credentials!');
Form validation error
if(!$validation->withRequest($this->request)->run()) {
$validationErrors = $validation->listErrors();
// Redirect back with validation errors
return redirect()->back()->with('error', $validationErrors);
}
Model:
Not used
View:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <h1>Login Super Admin</h1> <?php if (session()->get('error')): ?> <div class="alert alert-danger"> <?= session()->get('error') ?> </div> <?php endif; ?> <form action="<?php echo base_url(); ?>superadmin/login" method="post"> <input type="text" name="username"> <input type="password" name="password"> <button type="submit">LOGIN</button> </form> |
Password Helper:
app/Helpers/password_helper.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if (!function_exists('hash_password')) { function hash_password($password) { return password_hash($password, PASSWORD_BCRYPT); } } if (!function_exists('verify_password')) { function verify_password($input_password, $stored_hash) { return password_verify($input_password, $stored_hash); } } ?> |