Page 1 of 1

HELP: two concurrent session variable arrays

Posted: Tue Nov 02, 2010 4:01 am
by laowai
Hi,

I'm having a wierd problem related to session variables. It looks like variables would get duplicated inside same session, which of course should not be possible.

Background:
This problem exists on one page script which creating a quote request system where users can select different products and get quote for those. Quote system creates quote ID per customer and all products selected in one session should go under the same quote ID. Problem is that sometimes when user selects products second quote ID is created and after that products are randomly added into both of quotes. While trying to solve this problem I added simple logs to see what's going on, those are added below.

Problem seems to be caused by duplicated session variables. At somepoint it looks like session variables are not defined any more and new quote ID is created. After that the old and new session variables seems to be used randomly.

To demonstrate the problem I added session variable called 'counter' which is defined if it does not exist and incremented always when page is reloaded by submitting the form. As you can see from below logs the 'counter' variable seems to have duplicate values, see the quote value to separate two different session variable "sets".


<?php session_start();
echo "\n Current session id: ".session_id();
echo "\n _SESSION['quote']: ".$_SESSION['quote'];
$_SESSION['counter'] = isset($_SESSION['counter'])? $_SESSION['counter'] +1 : 0;
echo "\n _SESSION['counter']: ".$_SESSION['counter'];


Output when page is reloaded(form submitted):

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']:
$_SESSION['counter']: 0
set _SESSION['quote']: 984

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']:
$_SESSION['counter']: 0
set _SESSION['quote']: 985

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']: 985
$_SESSION['counter']: 1

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']: 985
$_SESSION['counter']: 2

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']: 984
$_SESSION['counter']: 1

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']: 985
$_SESSION['counter']: 3

Current session id: r5i15u4s9e20ud4j6jke8ln376
$_SESSION['quote']: 984
$_SESSION['counter']: 2


Could anyone explain how the 'counter' can behave such way inside same session (based on session ID)?
This is far beyond my knowledge and I would highly appreciate any advice or tip how to solve this porblem. Thanks.

BestRegards,
Laowai



---EDIT---
Added 'echo serialize($_SESSION);' to output session variables in the begin and end of the page to demonstrate problem more clearly.


<?php session_start();
echo "\nSerialized data at begin of page: ";
echo serialize($_SESSION);

echo "\n Current session id: ".session_id();
echo "\n _SESSION['quote']: ".$_SESSION['quote'];
$_SESSION['counter'] = isset($_SESSION['counter'])? $_SESSION['counter'] +1 : 0;
echo "\n _SESSION['counter']: ".$_SESSION['counter'];
OUTPUT:

Initial loading of page:
Serialized data at begin of page: a:0:{}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']:
$_SESSION['counter']: 0
...
Serialized data at end of page: a:1:{s:7:"counter";i:0;}


Page 1. reload by form submit:
Serialized data at begin of page: a:0:{}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']:
$_SESSION['counter']: 0
...
Serialized data at end of page: a:3:{s:7:"counter";i:0;s:8:"quote";i:1023;s:9:"quotedate";s:10:"2010-11-18";}


Page 2. reload by form submit:
Serialized data at begin of page: a:1:{s:7:"counter";i:0;}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']:
$_SESSION['counter']: 1
...
Serialized data at end of page: a:3:{s:7:"counter";i:1;s:8:"quote";i:1024;s:9:"quotedate";s:10:"2010-11-18";}


Page 3. reload by form submit:
Serialized data at begin of page: a:3:{s:7:"counter";i:0;s:8:"quote";i:1023;s:9:"quotedate";s:10:"2010-11-18";}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']: 1023
$_SESSION['counter']: 1
...
Serialized data at end of page: a:3:{s:7:"counter";i:1;s:8:"quote";i:1023;s:9:"quotedate";s:10:"2010-11-18";}

Page 4. reload by form submit:
Serialized data at begin of page: a:3:{s:7:"counter";i:1;s:8:"quote";i:1024;s:9:"quotedate";s:10:"2010-11-18";}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']: 1024
$_SESSION['counter']: 2
...
Serialized data at end of page: a:3:{s:7:"counter";i:2;s:8:"quote";i:1024;s:9:"quotedate";s:10:"2010-11-18";}

Page 5. reload by form submit:
Serialized data at begin of page: a:3:{s:7:"counter";i:1;s:8:"quote";i:1023;s:9:"quotedate";s:10:"2010-11-18";}
Current session id: vbbpohof2jo757eaj5jrp4dv02
$_SESSION['quote']: 1023
$_SESSION['counter']: 2
...
Serialized data at end of page: a:3:{s:7:"counter";i:2;s:8:"quote";i:1023;s:9:"quotedate";s:10:"2010-11-18";}
I hope this demonstrates my problem better than unclear original description. Sorry for that. This time "two concurrent" session variable arrays, if such can be, seem to be active one after another. Sometimes other is active few times and then another...

---EDIT---