Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MartinW12's avatar

Session flash doesn't work with multiple browser sessions open

Hello,

I am using "session()->flash()" to pass data outside of a form - ie. I flash some session data when I show the user the form, then pick up the flashed session data up when the user posts the form.

However I have come across a limitation which stops it working - if the user does something in another browser tab before posting the form, the flashed data is no longer available when they do post the form.

Obviously that is because the page on the other browser tab is the next request for this session.

Is it just a limitation of flashed session data that you can only use it before doing a "redirect()" and otherwise it assumes that the user will only ever have a single browser tab open at your website, or is there some way around this?

Thanks,

Martin

0 likes
3 replies
Snapey's avatar

Your flash data should come as the result of posting the form?, not set in session before the form is posted?

To have the situation you describe, the user would need to send the form then refresh the other tab before the result of the post was returned. Unless your form post takes several seconds, this seems unlikely?

MartinW12's avatar

To clarify, I have the following routes:

Route::get('/mytest', 'TestController@showForm'); Route::post('/mytest', 'TestController@postForm');

In "TestController::showForm()" I set a session variable with some data about the user's state when they started the test. I could put this in a hidden form field but I prefer to keep this data outside the form.

In "TestController::postForm()" I want to be able to retrieve the aforementioned data about the user's state .

What I have had to do is use normal session data and then delete the session data myself - I tried using "flash()" but it didn't work if the user had my website open in another tab and then did something in that other tab between being shown the form and posting the form (ie if they did something in the other tab while considering what data to enter into the form they have been shown).

Therefore my question was a general one as to whether this was a known limitation of flashing session data, or whether there is another way around this that I haven't thought of.

Snapey's avatar

Flash is for one request only. Store data in session, just use the session helper.

Bear in mind that the session is shared across all that user's logins within the same browser.

Please or to participate in this conversation.