首页 > 解决方案 > PHP会话数组在页面之间丢失

问题描述

因此,我已经为此工作了几天,现在试图弄清楚这一点,并在没有运气的情况下用谷歌搜索了它。我有一个将信息发布到 PHP 文档的 HTML 表单。我正在尝试获取该信息并将其推送到会话数组,然后将用户发送回 HTML 表单。我正在尝试使每次用户提交表单时,都会将新信息添加到会话数组中。但是,似乎每次新提交都会重置数组,因为当我 print_r 会话时仅反映最后一个条目,或者有时数组中根本没有出现任何内容。我也尝试过使用不同的浏览器和不同的机器。这是PHP代码:

<?php
session_start();

if (!isset($_Session['county'])) {
    $_SESSION['county'] = array();
} 

if (!isset($_Session['state'])) {
    $_SESSION['state'] = array();
} 

if (!isset($_Session['householdNumber'])) {
    $_SESSION['householdNumber'] = array();
} 

if (!isset($_Session['householdIncome'])) {
    $_SESSION['householdIncome'] = array();
} 

$dtmDateOfSurvey = $_POST["dtmDateOfSurvey"];
$strCountyAndState = $_POST["strCountyAndState"];
$intNumberInHousehold = $_POST["intNumberInHousehold"];
$dblHouseholdYearlyIncome = $_POST["dblHouseholdYearlyIncome"];

$arrCountyState = explode(',', $strCountyAndState);

$strCounty = $arrCountyState[0];
$strState = $arrCountyState[1];

array_push($_SESSION['county'],$strCounty);
array_push($_SESSION['state'],$strState);
array_push($_SESSION['householdNumber'],$intNumberInHousehold);
array_push($_SESSION['householdIncome'],$dblHouseholdYearlyIncome);

header('Location: index.html'); 
?>

标签: phparrayssession

解决方案


推荐阅读