Laravel 8 Login Session Example | GSS TECHNOLOGY
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 laravelbut 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.
@include('inner') <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> <div class="form-group" style="margin-left:400px;margin-right:400px;margin-top:30px"> <form action="user" method="post"> @csrf <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> <br><br> <button class="btn btn-primary" type="submit">Submit</button> </form> </div>
Please note i use the bootstrap for styling you can add the following file in inner.blade.php
Step 02: Create navbar.blade.php add the bootstrap navbar code like below.
@include('inner') <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> </button> <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> <li class="nav-item"> <a class="nav-link" href="/user">Login</a> </li> <li class="nav-item"> <a class="nav-link" href="/logout">Logout</a> </li> </ul> </div> </nav>
Step 03: Create index.blade.php and add the following code.
<!DOCTYPE html> <html> <head> <title>Laravel User Session Tutorial</title> </head> <body> <h1 style="color:blue;">Laravel User Session Tutorial</h1> @include('navbar') <h1>Welcome : {{session('username')}}</h1> </body> </html>
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("user",'user'); Route ::view("about","/about"); Route::view("logout",'user'); //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 Form Validation Example Advance | How to Use Middleware In Laravel With Example
Originally published at https://gss-technology.com on November 18, 2021.