Noam
Noam

Reputation: 3391

Popular reasons for inconsistent memcache session in php

I'm experiencing inconsistency in my $_SESSION variable which is defined to be saved in memcache. What are popular reasons for mixup between sessions values?

I'm using session_start() at the start of scripts, but sometimes the $_SESSION variables gets set to an older version.

Upvotes: 0

Views: 462

Answers (1)

PolishDeveloper
PolishDeveloper

Reputation: 950

It can be a session locking issue. With normal flow when PHP handles the request it blocks the session file from reading/writing (only this process can read/modify this file).

So when another request (lets say AJAX request) has to wait till first request finish and unlock the session.

With memcache session locking is probably disabled by default so in situation like that :

  1. AJAX request A comes and reads the session file (STATE X)
  2. AJAX request B comes and reads the session file (STATE X)
  3. AJAX request B ends and stores the modified session (changes state X to Y)
  4. AJAX request A ends and stores the modified session (changes state X to Z, forbids the the state Y)

In this case you will have session inconsistency, because request A will overwrite changes made by request B.

Upvotes: 1

Related Questions