Laravel 5.1 randomly discards session data

I have a strange problem with Laravel 5.1 application.

Intermittently, his session decline data. I discovered this by writing some middleware that writes the session contents for this request to a log file. Although the session identifier ( Session::getId() ) does not change, the _token value in the session data obtained using Session::all() is executed.

As I said, this happens intermittently. I can update the same URL several times, and then randomly update the remaining session data and _token values ​​different from previous requests.

What can cause this? Ive also noticed that the flash object is not in the “discarded” session data.

The following is a snippet of the log. You can see that the contents of the session_data key randomly change the “form” in the last two lines, but the session identifier remains constant.

Also, not sure if this is appropriate, but I have a DebugBar .

Screen shot

UPDATE:. While debugging, Ive found that on some page loads the session is completely empty, as in no _token (therefore, a new one is generated). Nothing.

+6
source share
2 answers
  • If you use a file driver, you may encounter race conditions with simultaneous requests. Then the file is truncated, Laravel cannot read it, so it updates the session. Race conditions can also lead to a symptom when something that you do for a session simply does not work out. This tends to be random, so it is very difficult to debug. According to the Laravel team, this is a known limitation of the file driver, and it doesn't seem to be fixed, so I would suggest using a different driver. This will fix your problem of random session updates, but it still adds the ability to make changes to a session that will not be added. As far as I know, at this point with Laravel 5.1 you have to do it yourself.

  • Somehow your session data is too long and truncated. If you use a database driver (other drivers have not been tested), and you try to save session data longer than the field length, subsequent queries cannot be extracted from this session, and you will have a new session. If this problem happens randomly with very short session data, then this is probably the reason mentioned above.

+2
source

If you are using Linux, try using Redis ( http://redis.io ) as a session / cache manager in laravel. In the past, I had some problems with text / cookie and laravel on some servers. When I installed Radish, I had no more problems.

Additional information: https://laravel.com/docs/5.1/redis

0
source

All Articles