PHP的会话不保存变量

时间:2018-07-03 15:49:48

标签: php session request slim

我正在尝试存储三个请求,然后将它们与我的大学项目进行比较。这是代码:

$app->map(['GET'],'/dados/{id}/{corrente}/{tensao}/{potencia}', function (Request $request, Response $response, array $args) {
if(!isset($_SESSION)) { 
    session_start();
    $_SESSION['req1'] = FALSE;
    $_SESSION['req2'] = FALSE;
    $_SESSION['req3'] = FALSE;
} 

require_once("db.php");

$req = array();

$id = $request->getAttribute('id');
$c = $request->getAttribute('corrente');
$v = $request->getAttribute('tensao');
$p = $request->getAttribute('potencia');

echo "id: $id, corrente: $c, tensão: $v, potência: $p \n";

array_push($req,$id,$c,$v,$p);

$query = $pdo->prepare('INSERT INTO teste_sustek VALUES (?,?,?,?)');
$query->execute([$id,$c,$v,$p]);

if (empty($_SESSION["req1"])){
    //first request
    $_SESSION["req1"] = $req;
    echo "first request \n";
    echo var_dump($_SESSION["req1"]);
} else if (empty($_SESSION["req2"])){
    //second request
    $_SESSION["req2"] = $req;
    echo "second request";
} else if (empty($_SESSION["req3"])){
    //compare them here
    $_SESSION["req3"] = $req;
    session_destroy();

    echo "third and last request";
} });

但是,当我通过Postman测试它时,总是得到以下输出,好像我的会话没有存储我的变量一样:

id:1,科伦特:2,tensão:3,potência:4

第一个请求

array(4){[0] =>字符串(1)“ 1” [1] =>字符串(1)“ 2” [2] =>字符串(1)“ 3” [3] => < / p>

string(1)“ 4”}

1 个答案:

答案 0 :(得分:1)

在您的index.php中调用session_start();