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
Laravel Framework

Login Form with Session in Laravel Framework

Install Laravel

  1. Download and install the latest version of Composer from https://getcomposer.org/download/

  2. Open Windows Terminal in Visual Studio Code install Laravel Installer with command as below:

    composer global require laravel/installer
  3. Create new folder named LearnLaravelWithRealApps. Use Visual Studio Code open to LearnLaravelWithRealApps foler. In this folder, create new project named LearnLaravelWithRealApps with command as below:

    laravel new LearnLaravelWithRealApps

  4. Run LearnLaravelWithRealApps project with command as below:

    php artisan serve
  5. Open LearnLaravelWithRealApps project with url as below:

    http://localhost:8000

Controllers

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
	public function index()
	{
		return view('demo/index');
	}

	public function welcome(Request $request)
	{
		$data = array(
			'username' => $request->session()->get('username')
		);
		return view('demo/welcome')->with($data);
	}

	public function processLogin(Request $request)
	{
		$username = $request->input('username');
		$password = $request->input('password');
		if ($username == 'abc' && $password == '123') {
			$request->session()->put('username', $username);
			return redirect('/demo/welcome');
		} else {
			$data = array(
				'msg' => 'Invalid'
			);
			return view('demo/index')->with($data);
		}
	}

	public function logout(Request $request)
	{
		$request->session()->forget('username');
		return redirect('/demo/index');
	}
}

Views

Demo Views

Create new folder named demo in resources/views folder. In demo folder, create new views as below:

Index View

Create new PHP file named index.blade.php in resources/views/demo as below:

<html>

	<head>
		<title>Laravel</title>
	</head>

	<body>

		<h3>Login</h3>
		{{ isset($msg) ? $msg : '' }}
		<form method="post" action="{{url('/demo/processlogin')}}">
			@csrf
			<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>

	</body>

</html>
Welcome View

Create new PHP file named welcome.blade.php in resources/views/demo as below:

<html>

	<head>
		<title>Laravel</title>
	</head>

	<body>

		Welcome {{$username}}
		<br>
		<a href="{{url('/demo/logout')}}">Logout</a>

	</body>

</html>

Routes

Open web.php file in routes folder, add new routes as below:

<?php
				
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\DemoController;

Route::group([], function () {
	Route::get('/', [DemoController::class, 'index']);
	Route::get('/demo', [DemoController::class, 'index']);
	Route::get('/demo/index', [DemoController::class, 'index']);
	Route::get('/demo/welcome', [DemoController::class, 'welcome']);
	Route::get('/demo/logout', [DemoController::class, 'logout']);
	Route::post('/demo/processlogin', [DemoController::class, 'processLogin']);
});

Structure of Laravel Project

Run Application

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

Output

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

Output

Test with valid account is username: abc and password: 123.

Output

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

Output

TaggedLaravel FrameworkLaravel SessionLogin Form with Session in LaravelSession in Laravel Framework

Related Posts

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

Post navigation

Previous Article Use Session in Laravel Framework
Next Article Flash Session in Laravel Framework

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