views:

493

answers:

4

Are session variables in the global $_SESSION array shared across scripts? Lets say I place a value into $_SESSION['box'], can another simultaneously running script store another value with the same key? Will they conflict? Or will it manage the actual variable values based upon the session ID from the client?

+1  A: 

Data stored in session is stored on a per-client basis -- the ID being generally passed by a cookie.

This means you cannot have two users accessing the same session ; it's not what it exists for.

=> 2 users / browsers / clients = 2 different sessions = 2 different values.


If you are asking about two scripts requested by the same use/client, and using file-based sessions (which is the default), the session-file of that user should be locked by the first script that begun, so two scripts don't access it at the same time -- which might cause problems if two scripts are trying to modify the session simultaneously.

=> 1 user, with 2 requests = one will be executed ; then, only, the second one will be executed ; means data set by the first one will be visible from the second one -- which will be executed only after the first one is finished.

Pascal MARTIN
+1  A: 

If I'm understanding you correctly, then the answer is the latter: Session variables are managed according to the session ID which is allocated to the client.

A 'session' refers to a user/browser-session. New user/browser, new session, new variables.

da5id
+4  A: 

If you have the same browser call 2 pages that set the same session variable, whichever one gets processed last will set the value.

If you have 2 separate browsers/users accessing the same 2 pages, they will set unique values.

Byron Whitlock
+1  A: 

It's unique per user session, but available globally within that user's session. If one script sets a variable with that key, and the second script being executed by the same user sets a variable with the same key, it is overwritten for just that user.

Rich