I store my session in database, because there are 5 different servers that need to share it.
My experience is, that session_start reads the session from the database, and it's written back to the database, when the execution of the php file is over.
Let's say initially the session content is ['data1' => 0, 'data2' => 0] and I have two ajax files: ajax1.php, that sets data1 to 1, and ajax2.php that sets data2 to 2.
They run simultaneously like this:
ajax1.php ----------------------
ajax2.php -------------
(So ajax2 starts running later, but finishes earlier)
In the situation I presented above, the following happens respectively:
- ajax1 reads session from db: data1 == 0 and data2 == 0
- ajax2 reads session from db: data1 == 0 and data2 == 0
- ajax1 sets its "local" superglobal $_SESSION: data1 = 1
- ajax2 sets its "local" superglobal $_SESSION: data2 = 2
- ajax2 writes the session to the database ['data1' => 0, 'data2' => 2]
- ajax1 writes the session to the database ['data1' => 1, 'data2' => 0]
This results that after they run the session contains ['data1' => 1, 'data2' => 0] when ['data1' => 1, 'data2' => 2] would be expected.
This is my problem, I need help:
- Has someone encountered the same situation?
- Is it "normal" that this happens, or it's my implementation which must have a problem?
- And of course, how should I deal with this situation?
- All other experience, thoughts, materials are welcome.
Thank you.