PHP会话和表单输入名称变得混乱

时间:2013-07-03 20:58:42

标签: php forms session post

我看到一些奇怪的行为:

我有2个会话,我设置了一些值:$_SESSION['shippingZip']$_SESSION['shippingOption']

然后我的代码通过此代码发布以表单形式发送的输入值:

$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];

这个帖子中的帖子空了。但是,Print显示我的会话$ _SESSION ['shippingOption']为空,它应显示先前已分配给它的字符串。 --------------------------张贴全部证明 会话加载了一些数据:

$_SESSION['shippingOption'] = $shippingOption;
$_SESSION['shippingZip']= $shippingZip;

然后:

Print_r ($_SESSION);
$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];
Print_r ($_SESSION);

输出:

Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => FIRST CLASS [shippingZip] => 10025 [shippingPrice] => 1.52 ) 
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => 10025 [shippingPrice] => 1.52 ) 
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => [shippingPrice] => 1.52 

您可以清楚地看到每次POST后,具有相同名称的SESSION失去其值。这太疯狂了!!!

1 个答案:

答案 0 :(得分:2)

$_POST未直接填充$_SESSION。您需要将值分配给会话

$_SESSION['shippingOption'] = $_POST['shippingOption'];

修改

发布更多代码后,在设置$shippingOption;之前,您似乎没有定义$_SESSION['shippingOption'] = $shippingOption;

确保订单如下:

$shippingOption = $_POST['shippingOption'];
$_SESSION['shippingOption'] = $shippingOption;