【问题标题】:Passing a multi-dimensional array using sessions in PHP在 PHP 中使用会话传递多维数组
【发布时间】:2020-06-26 23:55:57
【问题描述】:

我目前正在尝试通过会话传递多维数组,同时还能够动态地添加/删除它(这是一个愿望清单)。数组的索引将是我添加到数组中的项目的 ID。我尝试了序列化,使用变量而不是实际会话,但没有一个对我正常工作。

我的问题是我的数组不会从第 1 页传递到第 2 页。第 1 页是用户单击任何“添加到愿望清单”按钮时发生的情况

我在谷歌上搜索并写了类似的内容:

第 1 页:

session_start();
$_SESSION['wishlist'] = array();
$id = $_GET['id'];
$imageFileName = $_GET['ImageFileName'];
$title = urldecode($_GET['PictureName']);

$_SESSION['wishlist'][$id]=array("id"=>$id,"title"=>$title,"imageFileName"=>$imageFileName); // Here im making the multidimensional array

$_SESSION['wishlist'] = $_POST; //found this way on Stackoverflow

header('Location:view-wish-list.php'); //move to second page

第 2 页:尝试启动会话并打印出要测试的数组:

session_start();


var_dump($_SESSION['wishlist']);

Var Dump 给了我 array(0) { }

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 删除$_SESSION['wishlist'] = $_POST;。您正在发出 GET 请求,而 $_POST 显然是空的。

标签: php arrays session


【解决方案1】:

你必须在重定向之前commit (write)会话,否则第二个请求可能会在会话数据在服务器上可用之前发生:

session_write_close();
header('Location:view-wish-list.php'); //move to second page

【讨论】:

    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多