Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to avoid Duplicate User's Session Records in Session Table: Laravel

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?

like image 861
Pankaj Avatar asked Aug 16 '16 08:08

Pankaj


1 Answers

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);
   }
}
like image 160
Thibault Dumas Avatar answered Oct 20 '22 03:10

Thibault Dumas