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 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机