views:

119

answers:

1

I have a session handler class that calls session_write_close() at the end of the script. This insures that even if a header() or exit() is issued the session data is saved.

public function __destruct()
{
    session_write_close();
}

However, I have noticed that for one of my AJAX pages TWO session updates are committed by the database layer.

My guess is that the [1] page loads and sends an [2] AJAX request. That [2] AJAX request must start the session before the [1] page has a chance to call session_write_close().

After the [2] AJAX page has loaded the session then the [1] page finally saves the session and then shortly after the [2] AJAX request saves it's session - which overwrites the first one!

It might look like this:

[1] page loads session
[1] page sends output
[2] ajax loads session
[1] page saves session
[2] ajax sends output
[2] ajax saves session

What do I do to make sure one page isn't loading a session before another has a chance to save the session?

+1  A: 

The assumption was wrong.

...session data is locked to prevent concurrent writes only one script may operate on a session at any time http://us.php.net/session_write_close

In other words, you could be running 100 AJAX requests for a user at the same time and they would each wait their turn.

My problem turned out to be an error in selecting the right session table column resulting in my sessions being re-created each load.

Xeoncross
..which would explain the lack of info about this "problem"...
Xeoncross