手动设置会话ID后检索PHP会话变量

时间:2016-05-10 15:26:43

标签: php session

我有一个PHP脚本,可以手动设置PHP会话ID和变量。

但是,即使会话ID保持不变,也无法随后检索会话变量。

示例代码

if ($_GET['request'] == 0) {// new request
    echo "new request\n";
    $id = $_GET['id'];
    session_id(md5($id));
    session_start();
    $_SESSION['name'] = "joe";
    echo "SESSION CONTENT: ".json_encode($_SESSION);
    echo "\n";
    echo "SESSION ID: ".session_id();
    echo "\n";
}else { // subsequent requests
    echo "subsequent request\n";
    $id = $_GET['id'];
    session_id(md5($id));
    session_start();
    $name = $_SESSION['name'];
    echo "SESSION CONTENT: ".json_encode($_SESSION);
    echo "\n";
    echo "SESSION ID: ".session_id();
}

输出是:

新请求
会话内容:{“name”:“joe”} 会话ID:5cebd1165cf619fcc332998a087c6414
后续请求
会话内容:[]
会议ID:5cebd1165cf619fcc332998a087c6414

为什么$ _SESSION数组在后续请求中为空?

0 个答案:

没有答案