重定向后我的 $_SESSION 是空的,为什么? [英] After redirection my $_SESSION is empty, why?
问题描述
我运行了以下测试以验证我的代码是否正常工作:
I run followed test to verify my code working properly:
curl -L -i -X POST -d 'json={"orderBy":0,"maxResults":50}' http://mysite.com/ctlClient/
curl -L -i -X POST -d 'json={"orderBy":0,"maxResults":50}' http://mysite.com/ctlClient/
我调用:http://mysite.com/ctlClient/index.php:
session_unset();
session_start();
//pass data to _SESSION
foreach($_POST as $key => $value){
$_SESSION[$key] = $value;
}
// redirect to `/fwf/online/index.php`
ctlGotoSameDomain("/fwf/online/");
重定向后,我调用/fwf/online/index.php
:
<?php
session_start();
....
class fwfOnline {
public function __construct() {
msqLogFile('test/test_max',$_SESSION);
// here is my problem, $_SESSION is empty, :(
$this->json = isset($_SESSION['json']) ? $_SESSION['json'] : null;
global $gConfig;
if ($gConfig['soapDebug'])
msqLogFile("fwf/post", Array('post' => 'Request: '.$this->json));
$this->response = $this->getResponse();
echo $this->response;
}
....
在来自 mysite.com/ctlClient/index.php
的日志中,我看到 $_SESSION 有数据但在 /fwf/online/index.php
中它是空的.
In logs from mysite.com/ctlClient/index.php
I see that $_SESSION has data
but in /fwf/online/index.php
its empty.
谁能告诉我我错过了什么?
Can someone tell me what did i miss?
谢谢,
来自@rr-
我打印了两个会话 ID,结果有所不同:
I printed both session IDs and got difference:
"2013-07-05 09:44:31","Session ID: ihpfs1skrp792gncgancb02516"
"2013-07-05 09:44:31","Session ID: tp6ejtl1tj9bigrgsi3jt6h9a1"
为什么会这样?
[修复]
关于 @rr-
回答我发现了问题,
Regards to @rr-
answer I found the problem,
我需要将 -b
添加到我的脚本中以启用 cookie.CURL 默认不使用它们,这个问题会导致会话 ID 问题.
I need to add -b
to my script to enable the cookies. CURL by default doesn't use them and this issue caused to session ID problem.
谢谢rr-
推荐答案
确保您已在目标页面中启动会话:/fwf/online/index.php
.
Make sure you have started session in the target page: /fwf/online/index.php
.
您还可以在两个页面上检查 session_id()
的返回值.该值应该相同,如果即使您在两个页面上都调用了 session_start
,这可能意味着三件事:
You can also check return value of session_id()
on both pages. The value should be the same, if it isn't even though you have called session_start
on both pages, this can mean three things:
- 无论您调用
session_write_close
有效地导致创建新会话并忘记所有以前的数据, - PHP 无法访问它应该在服务器上存储会话数据的目录(它发生了!),
- ...或者用户禁用了 cookie,从而使他的浏览器无法保留会话 ID.
- Either you call
session_write_close
which effectively leads to creating new sessions and forgets all previous data, - PHP cannot access directory where it should store session data on the server (it happens!),
- ...or user has disabled cookies, thus making his browser unable to keep session ID.
我认为第三个选项很可能是问题的原因.
I think the third option is most likely the reason of the problem.
这篇关于重定向后我的 $_SESSION 是空的,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!