dongman5539 2013-08-26 21:55
浏览 60

CodeIgniter:重定向后购物车内容丢失

I am trying to develop a small web shop with CodeIgniter. To store the items, I use the Cart library. Most of the time, everything works great. However, sometimes the content of the cart gets lost after a redirect.

I found a couple of fixes on the web, but none of them works in my case. Here is my setup:

  • I use the DB for storing sessions
  • I don't use AJAX
  • There is no underscore in the name of the session cookie

Here is an example of a refresh:

public function add_item() {
    $item_id = $this->input->post('item');

    // Query database
    $item = $this->model->find_item($item_id);

    // Rewrite model info
    ...

    $data = array(
        'id'      => 'item-' . $item['id'] . '-size-' . $item['sizes'][$i]['id'],
        'qty'     => $qty,
        'price'   => $item['sizes'][$i]['price'],
        'name'    => $item['name'],
        'options' => array('short_name' => $item['short_name'])
    );

    $this->cart->insert($data);

    usleep(10000);
    redirect('shop');
}
  • 写回答

1条回答 默认 最新

  • dongshenyu4638 2013-08-27 21:31
    关注

    I finally found an answer to my problem, thanks to this question: CodeIgniter Cart and Session lost when refresh page

    The problem was that the data stored in the session got too big. CodeIgniter stores all the data in a cookie, which is limited to 4kB. My mistake was to think that, if I used the DB to store my sessions, I could avoid that limit. Apparently, CodeIgniter saves sessions in the database "only" for security reasons. There is still a lot of data in the cookie.

    Now, I use a library called Native session. I found it here: https://github.com/EllisLab/CodeIgniter/wiki/Native-session

    I just put the file in 'application/libraries', renamed the first function to '__construct()', added it to autoimport and replaced all the 'session' tags with 'native_session' in my code. I also had to change the Cart class because it was using CodeIgniter's original session.

    评论

报告相同问题?

悬赏问题

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