Skip to content

Learn Programming with Real Apps

Learn Programming with Real Apps

  • Home
  • .NET
    • ASP.NET Core MVC
    • ASP.NET Core Web API
    • Entity Framework Core
    • ASP.NET MVC
    • ASP.NET Core Razor Pages
    • ASP.NET Web API
    • Entity Framework
    • C#
  • Java
    • Spring Framework
      • Spring MVC
      • Spring Boot Data JPA
      • Spring Boot MongoDB
      • Spring Boot JDBC
      • Spring Boot Hibernate
      • Spring Data MongoDB
      • Spring JMS
      • Spring Rest API
    • JSF Framework
    • Struts Framework
    • JSP-Servlet
    • Hibernate
    • Java XML
    • JDBC
    • Java Restful Web Services
    • Java
  • Full Stack
    • Angular
    • React
      • React TypeScript
      • React Functional Components
      • ReactJS
    • ExpressJS
    • NestJS
    • MongoDB
    • ECMAScript
    • HTML, CSS, JavaScript
      • HTML 5
  • PHP & MySQL
    • PHP
    • Laravel Framework
    • CodeIgniter Framework
  • Mobile
    • Dart
    • Flutter
    • Android
  • Golang
    • Golang
    • GORM
    • Golang and MySQL
    • Golang and MongoDB
    • Golang RESTful Web API
    • Microservices
Main Menu
CodeIgniter Framework

Login Form with Session in CodeIgniter 4

Download and Install CodeIgniter 4

Download the latest version of CodeIgniter 4 and unzip source code to new folder named LearnCodeIgniter4WithRealApps

Cut index.php and htaccess files in public folder to root folder of project

Open index.php in root folder find to line 16 replace path to Paths.php file as below:

$pathsPath = realpath(FCPATH . '/app/Config/Paths.php');

Open App.php in app/Config folder find to line 39 remove index.php string in $indexPage variable as below:

public $indexPage = '';

Set BASE URL

Open App.php file in app/Config folder. Set value for $baseURL variable as below:

public $baseURL = 'http://localhost:8091/LearnCodeIgniter4WithRealApps/';

Create Controller

Create new PHP file named Demo.php in app/Controllers folder as below:

<?php

namespace App\Controllers;

class Demo extends BaseController
{
	public function __construct()
	{
		helper(['url', 'form']);
	}

	public function index()
	{
		return view('demo/index');
	}

	public function process_login()
	{
		$username = $this->request->getPost('username');
		$password = $this->request->getPost('password');
		if ($username == 'pmk' && $password == 'lab') {
			session()->set('username', $username);
			return $this->response->redirect(site_url('demo/welcome'));
		} else {
			$data['error'] = 'Invalid Account';
			return view('demo/index', $data);
		}
	}

	public function welcome()
	{
		return view('demo/welcome');
	}

	public function logout()
	{
		session()->remove('username');
		return $this->response->redirect(site_url('demo/index'));
	}
}					

Create Views

Create new folder named demo in app/Views folder. In this folder, create new PHP file named as below:

Index View

In demo folder, create new PHP file named index.php as below:

<html>

	<head>
		<title>Login Form in CodeIgniter 4</title>
	</head>

	<body>

		<h3>Login Form</h3>
		<?= isset($error) ? $error : '' ?>
		<?= form_open('demo/process_login') ?>
			<table cellpadding="2" cellspacing="2">
				<tr>
					<td>Username</td>
					<td>
						<input type="text" name="username" required="required">
					</td>
				</tr>
				<tr>
					<td>Password</td>
					<td>
						<input type="password" name="password" required="required">
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>
						<input type="submit" value="Login">
					</td>
				</tr>
			</table>
		<?= form_close() ?>

	</body>

</html>
			

Success View

In demo folder, create new PHP file named success.php as below:

<html>

	<head>
		<title>Login Form in CodeIgniter 4</title>
	</head>

	<body>

		Welcome <?= session('username') ?>
		<br>
		<a href="<?= site_url('demo/logout') ?>">Logout</a>

	</body>

</html>
			

Define Routes

Open Routes.php file in app/Config folder. Set default controller as below:

$routes->get('/', 'Demo::index');
$routes->get('/demo/welcome', 'Demo::welcome');
$routes->get('/demo/logout', 'Demo::logout');
$routes->post('/demo/process_login', 'Demo::process_login');

Structure of CodeIgniter 4 Project

Run Application

Access index action in Demo controller with following url: http://localhost:8091/LearnCodeIgniterWithRealApps/demo/index

Output

Test with invalid account is username: abc and password: 123

Output

Test with valid account is username: pmk and password: lab.

Output

Click logout link from success page to remove session and open login page again

Output

Taggedform login in codeigniterLogin Form with Session in CodeIgniter

Related Posts

Multiple Submit Buttons in CodeIgniter 4

December 20, 2021November 21, 2024

Create Data from Database in Query Builder Class in CodeIgniter 4

December 12, 2021November 21, 2024

Avg Data from Database in Query Builder Class in CodeIgniter 4

December 9, 2021November 21, 2024

Post navigation

Previous Article Session in CodeIgniter 4
Next Article Ajax in CodeIgniter 4

Latest Posts

Declare Variables with Dynamic Keyword in Dart

December 1, 2024December 1, 2024

Declare Variables with Var Keyword in Dart

December 1, 2024December 1, 2024

Declare Variables with Data Types in Dart

December 1, 2024December 1, 2024

Middleware in Laravel

July 21, 2022November 18, 2024

Shopping Cart with Session and Eloquent ORM in Laravel

July 19, 2022November 18, 2024

Multi Select AutoComplete Search from Database with Eloquent ORM in Laravel Framework

July 5, 2022November 18, 2024

Archives

  • December 2024 (3)
  • July 2022 (10)
  • June 2022 (57)
  • January 2022 (1)
  • December 2021 (11)
  • November 2021 (30)
  • October 2021 (8)
  • February 2021 (24)
  • January 2021 (38)
  • May 2020 (6)
  • April 2020 (17)
  • November 2019 (15)
  • September 2019 (47)
  • August 2019 (7)
  • July 2019 (35)
  • May 2019 (44)
  • April 2019 (62)
  • March 2019 (63)
  • January 2019 (6)
  • December 2018 (29)
  • November 2018 (93)
  • October 2018 (41)
  • September 2018 (76)
  • August 2018 (82)
  • July 2018 (92)
  • June 2018 (131)
  • May 2018 (5)
  • April 2018 (59)

Popular Tags

Aggregate in CodeIgniter ajax in codeigniter Ajax in Laravel Framework ASP.NET Core MVC Average in Eloquent ORM CodeIgniter Codeigniter Form Validation CodeIgniter Framework CodeIgniter Model and Entity Count in Eloquent ORM CrudRepository Interface in Spring Boot MongoDB Date in Eloquent ORM Declare Variables in dart Dynamic Type in C# Eloquent ORM Eloquent ORM in Laravel Eloquent ORM in Laravel Framework Equal Operator in Eloquent ORM Form Validation CodeIgniter GroupBy in Eloquent ORM Having in Eloquent ORM Laravel Ajax Laravel Session Laravel Validation limit in Eloquent ORM limit in mongodb Max in Eloquent ORM Min in Eloquent ORM MongoDB mongodb in spring boot MongoDB in Spring Data JPA ORM in CodeIgniter PagingAndSortingRepository in Spring Data JPA Passing data from controller to view in Laravel Query Builder Class in CodeIgniter Query Builder in CodeIgniter Relationship in Eloquent ORM in Laravel Framework Session in Laravel Framework Sort in Eloquent ORM sort in mongodb Spring Boot MongoDB sum in Eloquent ORM Transfer Data from Controller to View in Laravel Validation Rule in CodeIgniter Validation Rule in Laravel Framework

Copyright © 2025 learningprogramming.net