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

Drewlim7's avatar

How to have unique session id per web browser's tab

Hi, I am creating a multistep form which consists of 3 forms. Each form has a next button with ajax post request to send data and store data in backend. I am using $request->session()->put('camp_id','camp_id(generated once data stored in db'). by using this method, I can store the remaining forms with the session camp_id. session will be removed once user completes the third form. However, I just discovered some loophole when user open a new tab and doing the same step. Both session key is the same but the camp_id should be different (I assumed).

Another problem is when user quit the tab when user finished the first form (camp_id is generated and store in db), then user clicks add new camp which still using the same camp_id (by right the camp_id should use a new camp_id after user completes the first form). So I am trying to solve this by removing the session key when the view is loaded. However, there is another problem when user finishes the first form then manually click the back button from browser and click next to continue the form. When user comeback from back button, my solution will eventually remove the session key and the form could not be processed due to camp_id is empty when user try to store the 2nd form.

Thank you in advance for solving my problem.

0 likes
2 replies
alanholmes's avatar
Level 35

@drewlim7

You are going to struggle if you keep the ID in a session, very much so with the problems you have suggested.

You could maybe look into using the window.unloadto fire an ajax request that will clear the session, only anything but a next click - but tbh, I am not sure how reliable that would be.

Personally, for a multistep form, I would use something like https://binarcode.github.io/vue-form-wizard/#/ so the form is all within a single page, and just shown/hidden dependant on the step.

If you wanted to persist after each step, you could still fire the ajax request, and then just store the resultant ID in the hidden field and/or a JS variable, to pass along with the next request.

Another option, would be to pass the id via a querystring/post to the next page, but you would need to validate this against the user before continuing, and also if not present, they would have to be redirected back to the first step.

Drewlim7's avatar

Hi, Sorry for the late reply. I think I will store the resultant ID in the hidden field and check every the ID with db result before proceed to the request. Thank you very much!

Please or to participate in this conversation.