For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Signin in CodeIgniter 4

Overview

In this example, we will discuss the Login example using CodeIgniter.

Here, we are using 3 files for generating PDF file:

Controllers: 	Signin.php		app\Controllers\Signin.php
                Home.php		app\Controllers\Home.php
Models: 	SigninModel.php         app\Models\SigninModel.php
Filters:        AuthFilter.php          app\Filters\AuthFilter.php
View:		signin.php              app\Views\signin.php

app/Controllers/Signin.php

<?php
namespace App\Controllers;

use App\Models\SigninModel;

class Signin extends BaseController {
    
    public function __construct() {
		$db = db_connect();
		helper(['url', 'form', 'array']);
		$this->session 	= \Config\Services::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');
        }
    }
    
}

app/Models/SigninModel.php

<?php
namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;

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;
        }
        
    }
}

app/Filters/AuthFilter.php

<?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()->get('login_status')){
          return redirect()->route('login');
        }

    }

    //--------------------------------------------------------------------

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

app/Views/signin.php

<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 class="checkbox icheck">
				<label>
					<!-- <input type="checkbox">Remember Me</label> -->
			</div>
		</div>
		<!-- /.col -->
		<div class="col-xs-4">
			<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
		</div>
		<!-- /.col -->
	</div>
</form>

app/Controllers/Home.php

<?php
namespace App\Controllers;

class Home extends BaseController {

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

}

app/Config/Routes.php

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