PHP:会话中的异步或并发会话变量数组

时间:2010-11-01 10:04:16

标签: php session variables

我对PHP会话变量有一个非常奇怪的问题。基于会话ID,看起来在同一会话中会有双变量,这自然是不可能的。

问题是当提交页面中的表单重新加载页面本身时,'quote'会话变量应保持相同。 $ _SESSION ['quote']变量只有在未定义时才会设置,前两次重新加载会发生什么,如下面的日志所示。

调试代码:

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; 

Firefox和IE会出现此问题。 任何建议或提示将受到高度赞赏。 提前谢谢。

--- --- EDIT 添加了echo serialize($ _ SESSION);提议。

<?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'];

输出:

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";}

我希望这比不清楚的原始描述更能说明我的问题。对不起。这次“两个并发”的会话变量数组,如果可以的话,似乎一个接一个地处于活动状态。有时其他人活跃几次,然后是另一次......

--- --- EDIT

3 个答案:

答案 0 :(得分:0)

所有人都很奇怪,“设置_SESSION”来自哪里? 脚本是否同时作用于post和get?

确保它在两个方法上都达到session_start()。

答案 1 :(得分:0)

劳威,很乐意提供帮助,

首先,您的代码有错误:

isset($_SESSION['counter']) ? $_SESSION['counter'] +1 : 0;

应该是

isset($_SESSION['counter']) ? $_SESSION['counter']++ : $_SESSION['counter'] = 0;

其次,如果你仍然有生产服务器错误,请检查access-log以追踪任何意外的请求(来自jQuery或其他)弄乱你的控制器(?) - 请求。

第三,我建议您在顶部读取$ _SESSION数据ONCE并复制到常规$ var,并在脚本结束时写回$ _SESSION。 $ _SESSION通常是文件,在高负载的服务器上可能会发生奇怪的事情。

$sesscopy = $_SESSION;
//do everything...
//now done...
$_SESSION = $sesscopy;

的问候, / t eriksson

答案 2 :(得分:0)

我创建了一个简单的脚本来模拟我在页面的开头和结尾转储会话变量的问题。页面有单个按钮,它将刷新页面,在页面的开头是计数器,存储在会话变量中。发现如果快速按下按钮一切正常,但如果等待> 10秒,则会话变量为空(计数器变量),然后按下按钮时原始计数器或并发会话变量数组计数器递增。

我还将问题隔离到托管原始有问题网站的服务器中。问题无法在localhost或我测试的其他服务器中重现。所以结论是服务器中的某些配置是错误的,或者PHP 5.2.9或其他组件中存在错误。

谢谢大家的意见。