user399666
user399666

Reputation: 19879

"Simultaneous" requests generate new PHP session ID

I have a PHP script that dynamically generates JavaScript. I include it like you would include any external JS file (I use mod rewrite to give it a JS extension and I set the JS content header).

In certain cases, I want to be able to include this JS file multiple times on the same page (various different IDs are being passed through via the query string of the URL). Example:

<script src="script.js?id=3982"></script>

On the first page view, I've noticed that three unique sessions IDs are being generated (one for each request). It seems as though the last script to be included is the script that generates the final session ID that the user keeps. From there on, everything works fine.

Note: session_start() is included at the top of each script.

Upvotes: 0

Views: 231

Answers (1)

deceze
deceze

Reputation: 522042

Well, yes, since the browser will attempt to load several scripts in parallel, and doesn't have a session cookie yet, it'll send off several id-less requests in parallel, which will all receive independent session ids. There's no real solution besides de-parallelising the requests. Meaning, include only one script which makes one initial connection to receive a cookie, then include the other scripts.

Upvotes: 3

Related Questions