Search code examples
laravellaravel-session

Laravel 6 Session does not persist in web.php


Both of the files I put together in web.php so that they share the same web middleware. But the session does not Store

I am create a SMS OTP system. In the API\SmsController@create function I run

$rand = rand(100000,999900);

session()->put('otp_test', $rand);
session()->keep(['otp_test']);

After the API request, I can see from Laravel Debugger in the SESSION tab have the session variable.

However, when I refresh the page, the SESSION variable is not there . It only leave

_token                  Zv3IpiLwwIXTUMc4tMW1J9eJA5lJCliGtdwEvx0e
_previous               array:1 [ "url" => "http://sms.test/register" ]
_flash                  array:2 [ "old" => [] "new" => [] ]
url                     array:1 [ "intended" => "http://sms.test" ]
PHPDEBUGBAR_STACK_DATA

enter image description here Also when I do the Form Post, session('otp_test') also NULL.

After I refresh the page. There is opt_test session there. enter image description here

I tried file, database in session.php both also not persist when I refresh or go to next page. It seem like flash it after all.


Solution

  • The keep method is for flashed session data. Flashed session variables will end up getting removed. You just turned your regular session variable (that would exist until you remove it, the session is flushed or dies) into a flashed variable that will get removed automatically by calling keep like that.

    Laravel 6.x Docs - Sessions - Flash Data