-3

我看到一些奇怪的行为:

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

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

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

这些 POST 在此通道中将变为空。但是,当它应该显示之前分配给它的字符串时,打印显示我的会话 $_SESSION['shippingOption'] 为空。-------------------------- POSTING FULL PROOF 会话加载了一些数据:

$_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 是如何失去其价值的。简直太疯狂了!!!

4

1 回答 1

2

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

IE

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

编辑

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

确保顺序如下:

$shippingOption = $_POST['shippingOption'];
$_SESSION['shippingOption'] = $shippingOption;
于 2013-07-03T21:02:43.440 回答