Single Session Login in Laravel
I recently did this.
My solution was to set the session value when a user logs in. Then I had a small class checking if the session ID stored is the same as the current user who is logged in.
If the user logs in from somewhere else the session ID in the DB will update and the "older" user will be logged out.
I didn't alter the Auth driver or anything, just put it on top when the user logs in. Below happens when login is successful:
$user->last_session = session_id();
$user->save();
To check if the session is valid I used below
if(session_id() != Auth::user()->last_session){
Auth::logout();
return true;
}
As you can see I added a column in the users table called last_session
Laravel: Only allowing one session per user at a time
Yeah, I did similar for my project.
So, you need add to your user model another attribute in this case: last_sessid.
public function swapping($user) {
$new_sessid = \Session::getId(); //get new session_id after user sign in
$last_session = \Session::getHandler()->read($user->last_sessid); // retrive last session
if ($last_session) {
if (\Session::getHandler()->destroy($user->last_sessid)) {
// session was destroyed
}
}
$user->last_sessid = $new_sessid;
$user->save();
}
Now, if the user has an active session, and signs in another browser, the first session will be removed.
P.S. Sorry for my bad english :)
Restrict User to Single and Multiple Login in Laravel
I was able to solve it by creating a Session table
php artisan session:table
composer dump-autoload
php artisan migrate
Then in my controller
public function authenticated(Request $request,User $user)
{
if(Auth::check())
{ //check if the user is logged in or not
$user = Auth::user();
// $login = Session::where('user_id', Auth::id())->count();
$login = DB::table('sessions')->where('user_id', Auth::id())->count();
// dd($login);
if ($user->isBasic())
{
if ($login > 0)
{
Auth::logout();
session()->flash('logout', "You are Logged in on other devices");
return redirect('login');
}
return redirect(route('welcome'));
}
elseif ($user->isCouple())
{
if ($login > 1)
{
Auth::logout();
session()->flash('logout', "You are Logged in on other devices");
return redirect('login');
}
return redirect(route('welcome'));
}
elseif ($user->isFamily())
{
if ($login > 5)
{
Auth::logout();
session()->flash('logout', "You are Logged in on other devices");
return redirect('login');
}
return redirect(route('welcome'));
}
}
else
{
return redirect(route('welcome'));
}
}
Related Topics
Laravel Quick Start Guide Route Not Working
Move All Files in a Folder to Another
PHP Dom Get Nodevalue HTML? (Without Stripping Tags)
Symfony\Component\Httpkernel\Exception\Notfoundhttpexception Laravel
How to Create a New Joomla User Account from Within a Script
How to Send Notification to Android from PHP
Changing a Global Variable from Inside a Function PHP
Php: Limit Foreach() Statement
Fetching Mail from a Pop3 Server Using PHP
PHP Rename Array Keys in Multidimensional Array
Returning Header as Array Using Curl
Simple Comet Example Using PHP and Jquery
How to Create a New Joomla User Account from Within a Script
Php: Catch Exception and Continue Execution, Is It Possible