13

Sample data in this table looks like below:

enter image description here

There are multiple duplicate User's Session records present in the table.

vendor\laravel\framework\src\Illuminate\Session\DatabaseSessionHandler.php

In the above file path, we have below method

public function write($sessionId, $data)
{
    $payload = $this->getDefaultPayload($data);

    if (! $this->exists) {
        $this->read($sessionId);
    }
    if ($this->exists) {
        $this->getQuery()->where('id', $sessionId)->update($payload);
    } else {
        $payload['id'] = $sessionId;

        $this->getQuery()->insert($payload);
    }

    $this->exists = true;
}

It checks for Session ID.

Question

Can I avoid creation of duplicate User Session Records in Session Table? Is there any flag that do so in Session Config file?

8
  • 1
    This allows a user to log from many different devices and/or browsers, so he can access the app from phone and computer for example. You want to log out the user from all other instances when he logs in? Commented Aug 18, 2016 at 18:46
  • I just want to keep one session. Commented Aug 18, 2016 at 18:48
  • btw would this be helpful? stackoverflow.com/questions/33311771/… Commented Aug 18, 2016 at 18:49
  • 2
    What's the purpose? Why do you want to revive old sessions? Commented Aug 18, 2016 at 21:52
  • @Helper Are you use build-in auth (artisan make:auth)? Can i edit \App\Http\Auth\AuthController for this purpose? Commented Aug 21, 2016 at 16:23

2 Answers 2

1

It seems to be an error in your traitement, must be like this no ? :

 if (! $this->exists) {
    $this->read($sessionId);
}else{

   if ($this->exists) {
       $this->getQuery()->where('id', $sessionId)->update($payload);
   } else {
       $payload['id'] = $sessionId;
       $this->getQuery()->insert($payload);
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry to say, I think this code is the same which is present already in my question?
0

From your question, you want only leave one user session in database, which means one user can only login from one device, example if you already logined from chrome , then if you login from firefox, your chrome login status will be removed.

To acheive this you can write a function in App\Http\Controllers\Auth\AuthController:

public function authenticated(Request $request,User $user){
    $previous_session = $user->session_id;

    if ($previous_session) {
    \Session::getHandler()->destroy($previous_session);
    }

    Auth::user()->session_id = \Session::getId();
    Auth::user()->save();
    return redirect()->intended($this->redirectPath());
}

this function will destory prvious session from database before login. for more info you should check Trait :Illuminate\Foundation\Auth\AuthenticatesUsers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.