Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4
About Lesson

In this article, we will discuss, How to create a Login with MySQL database using the CodeIgniter 4.

1. Copy and Paste the following lines in app/Config/Routes.php

$routes->get('/', 'Home::index', ['filter' => 'auth']);
$routes->add('login', 'Signin::login');
$routes->post('signin', 'Signin::signin');

2. Create a signin.php file in the app/Views/ directory

<p class="login-box-msg">Sign in to start your session</p>
<form action="<?php echo base_url('signin')?>" method="post" autocomplete="off">
    <div class="form-group has-feedback">
        <input type="email" name="email" id="name" class="form-control" placeholder="Email"> 
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <input type="password" id="password" name="password" class="form-control" placeholder="Password"> 
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
    </div>
    <div class="row">
        <div class="col-xs-8">
        </div>
        <div class="col-xs-4">
            <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
        </div>
    </div>
</form>

3. Create a model file SigninModel.php in the app/Models/ directory.

<?php
namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\DatabaseConnectionInterface;

class SigninModel extends Model {

    protected $db;
    public function __construct(ConnectionInterface &$db) {
        $this->db =& $db;
    }

    public function clientLogin($email, $password) {
        $result = $this->db
                        ->table('user_info')
                        ->where(["status" => "1", "email" => $email, "password" => $password])
                        ->get()
                        ->getRow();
        if($result) {
            $data = [
                'login_id'          => $result->id,
                'login_name'        => $result->name,
                'login_email'       => $result->email,
                'login_status'      => TRUE,
            ];
            return $data;
        } else {
            return 0;
        }
        
    }
}

4. Create a controller file Signin.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

use App\Models\SigninModel;

class Signin extends BaseController {
    
    public function __construct() {
        $db = db_connect();
        helper(['url', 'form', 'array']);
        $this->session   = ConfigServices::session();
        $this->loginModel = new SigninModel($db);

    }
    
    public function index() {
        $this->login();
    }
    
    public function login() {
        echo view("signin");
    }

    public function signin() {
        $email      = $this->request->getPost('email');
        $password   = $this->request->getPost('password');

        $result     = $this->loginModel->clientLogin($email, md5(md5($password)));
        if($result) {
            $this->session->set($result);
            return redirect()->route('home/');
        } else {
            return redirect()->route('login');
        }
    }
    
}

5. Create a controller file Home.php in the app/Controllers/ directory.

<?php
namespace App\Controllers;

class Home extends BaseController {

    public function __construct() {
        helper(['url', 'form', 'array']);
        $this->session   = ConfigServices::session();
    }
    
    public function index() {   
        echo "Welcome";
    }

}

6. Create a controller file AuthFilter.php in the app/Filters/ directory.

<?php
namespace AppFilters;

use CodeIgniterHTTPRequestInterface;
use CodeIgniterHTTPResponseInterface;
use CodeIgniterFiltersFilterInterface;

class AuthFilter implements FilterInterface {

    public function before(RequestInterface $request, $arguments = null) {
        if(! session()->get('login_status')){
          return redirect()->route('login');
        }

    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) {
        // Do something here
    }
}