Today, We want to share with you Laravel 5.6 Prevent Block Multiple Login Of Same Credentials.In this post we will show you Laravel prevent multiple logins from same user, hear for how to prevent multiple login for same user in Laravel we will give you demo and example for implement.In this post, we will learn about Laravel 5.6 – Prevent Block Multiple Login Of Same Credentials with an example.
Laravel 5.6 Prevent Block Multiple Login Of Same Credentials
There are the Following The simple About Laravel 5.6 Prevent Block Multiple Login Of Same Credentials Full Information With Example and source code.
As I will cover this Post with live Working example to develop how to prevent multiple login for same user in Laravel, so the prevent multiple logins in Laravel website for this example is following below.
Step 1: Laravel Migration
Update and Add some fields in Members Table Migration:
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateMembersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('members', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->string('session_id'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('members'); } }
//Laravel migration using following command php artisan migrate
//Create Laravel Auth: php artisan make:auth
Change in SigninController.php File:
app/Http/Controllers/Auth/SigninController.php
namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesMembers; use Illuminate\Http\Request; use App\Http\Requests; use App\User; use DB; class SigninController extends Controller { use AuthenticatesMembers; /** * Where to redirect members after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function login(Request $request) { $this->validate($request, [ 'email' => 'required', 'password' => 'required', ]); $member = \DB::table('members')->where('email', $request->input('email'))->first(); if (auth()->guard('web')->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) { $new_sessid = \Session::getId(); //get new session_id after member sign in if($member->session_id != '') { $last_session = \Session::getHandler()->read($member->session_id); if ($last_session) { if (\Session::getHandler()->destroy($member->session_id)) { } } } \DB::table('members')->where('id', $member->id)->update(['session_id' => $new_sessid]); $member = auth()->guard('web')->member(); return redirect($this->redirectTo); } \Session::put('login_error', 'Sorry, Your email and password wrong!!'); return back(); } public function logout(Request $request) { \Session::flush(); \Session::put('success','Good Lcuk, you are logout Successfully'); return redirect()->to('/login'); } }
Include in app.blade.php File:
resources/views/layouts/app.blade.php
Run Your Project
php artisan serve http://localhost:8000/login
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Laravel 5.6 Prevent Block Multiple Login Of Same Credentials.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.