I am trying to save a session in my controller:
public function index(Request $request)
{
$my_session_value = $request->session()->get('my_session');
if (is_null($my_session_value))
{
$request->session()->put('my_session', 'value');
Log::debug("The session value was null");
}
Log::debug("Session after setting value: " . $request->session()->get('my_session'));
}
But randomly, and very often, the session would not be saved.
For example I can refresh the index view multiple times, and the log would look like that:
['2024-08-10 14:14:25'] dev.DEBUG: The session value was null
['2024-08-10 14:14:25'] dev.DEBUG: Session after setting value: value
['2024-08-10 14:14:27'] dev.DEBUG: The session value was null
['2024-08-10 14:14:27'] dev.DEBUG: Session after setting value: value
['2024-08-10 14:14:30'] dev.DEBUG: The session value was null
['2024-08-10 14:14:30'] dev.DEBUG: Session after setting value: value
As if Laravel went inside the is_null()
check, saved the session, then I logged the newly saved session (before the request finished), and it showed the actual value that was supposed to be saved, but something interrupts it in the middle and doesn’t let it finish saving it.
Then if I do other actions on the page the session would be saved.
But what is interrupting it so many times? I cannot find a way to debug it.
Could that be related to other things like the environment configuration? The fact that I’m using IIS on Windows with Non-Thread-Safe version of PHP instead of using Linux? Mis-configuring the application pool on IIS? Something else inside Laravel?