Laravel 8 Login Session Example

Laravel 8 Login Session Example

The user session in web application is a piece of user information which developer can store it into the browser for later use .

You can add the following simple steps to create user session and use it in your project. There are many method to use session in laravel but we now cover the basic user session in laravel.

For creating the user session in Laravel you need to create one view and apply some condition in routes.

Step 01: Create userlogin.blade.php in view directory. Add the following code in your user login page.

<div style="text-align: center; color:white;background-color:#34495e; margin-left:400px;margin-right:400px; margin-top:20px; border:1px;border-radius:2%;">
<h1>User Login Page</h1>
<div class="form-group" style="margin-left:400px;margin-right:400px;margin-top:30px">
	<form action="user" method="post">
	<input type="text" class="form-control" name="username" placeholder="Enter Usernme"><br>
	<span style="color: red;">@error ('username'){{$message}} @enderror</span>
	<input type="password" class="form-control" name="password" placeholder="Enter Password"><br>
	<span style="color: red;">@error ('password'){{$message}} @enderror</span>
	<button class="btn btn-primary" type="submit">Submit</button>

Please note i use the bootstrap for styling you can add the following file in inner.blade.php

<link rel="stylesheet" href="" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

Step 02: Create navbar.blade.php add the bootstrap navbar code like below.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="/index">Home <span class="sr-only">(current)</span></a>
      <li class="nav-item">
        <a class="nav-link" href="/user">Login</a>
      <li class="nav-item">
        <a class="nav-link" href="/logout">Logout</a>

Step 03: Create index.blade.php and add the following code.

<!DOCTYPE html>

    <title>Laravel User Session Tutorial</title>


    <h1 style="color:blue;">Laravel User Session Tutorial</h1>

    <h1>Welcome : {{session('username')}}</h1>



Step 04: Open web.php in routs directory add add the following routes .

Route::get('/', function () {
    return view('index');


Route ::view("contact","/contact");
Route ::view("about","/about");
//condition for check login if user login he can't go to the user login page
Route::get('/user', function () {
	if (session()->has('username'))

		return redirect ('/');
    return redirect ('user');

//if user login then he can't go to login page and return to home page
Route::post("user",[UserAuth::class, 'UserLogin']);

Output Is:

Laravel 8 Login Session Example
Laravel 8 Login Session Example

Laravel Form Validation Example Advance | How to Use Middleware In Laravel With Example

This Post Has 2 Comments


    Welcome to the world of adult Dating

Leave a Reply