douliu3831 2012-04-27 21:01
浏览 67
已采纳

会话数组在服务器上被区别对待/覆盖,但在本地XAMPP上没有

I have a site that stores an array in sessions. Items are added to the array and the array is looped through to display the items.

On my local XAMPP it is working fine but now I've uploaded it, it is showing some odd behaviour.

My array should look like this:

Array
(
    [bag] => Array
        (
            [0] => Array
                (
                    [item_id] => 4
                    [stock_id] => 7
                    [quant] => 1
                )
         )
)

And after I add an item it does but after a few clicks it turns in to this:

Array
(
    [bag] => 1
)

with the error:

Warning: Invalid argument supplied for foreach() in /public_html/xxx.php on line xx

Is there some session setting I should be aware of for the php .ini configuration or something similar? (I'm using Hostgator if that helps)


A simplified way I'm adding items to the array-

function AddBag()
{
//get info
    $item_id   = $_POST['item_id'];
    $quant      = $_POST['quant];
    $stock_id  = $_POST['stock_id];

    if (isset($_SESSION['bag'])) {$bag_array = $_SESSION['bag'];}
    else                         {$bag_array = array();}

//add item into array
    $new_item = array("item_id" => $item_id, "stock_id" => $stock_id, "quant" =>         $quant);
    array_push($bag_array, $new_item);

//update session with new array
    $_SESSION['bag'] = $bag_array;
}
  • 写回答

1条回答 默认 最新

  • doushajian2018 2012-04-27 22:36
    关注

    Think I've found the answer myself-

    I have a foreach loop like this:

    foreach ($_SESSION['bag'] as $bag) {$bag_num = $bag_num + $bag['quant'];
    

    This worked fine testing it on my local XAMPP but on my hosting it wasn't having it. Changing it to:

    foreach ($_SESSION['bag'] as $bagx) {$bag_num = $bag_num + $bagx['quant'];
    

    seems to work though.

    I can see why there would be a conflict but not WHY it works on one or on the other, any ideas?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置