使用会话使用 CodeIgniter 存储购物车 [英] Using sessions to store a shopping cart using CodeIgniter

查看:45
本文介绍了使用会话使用 CodeIgniter 存储购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个电子商务网站,不想强迫用户拥有一个帐户来购买商品,他们可以随时购买.

I want to make an e-commerce website and don't want to force users have an account to purchase items, they can just do it whenever they feel like it.

我能否使用 CodeIgniter 中的会话类来执行此操作,并能够将用户会话(即他们的购物车项目)保持较长时间(可能是 6 个月或一年)?我阅读了有关会话的 CI 用户指南,它说它通过在配置文件中将 session_expire_on_close 变量声明为 false 为您提供了将会话设置为不过期的选项,我猜即使在用户关闭后,会话仍会继续运行浏览器.

Can I use the sessions class in CodeIgniter to do this, with the ability to keep a users session (i.e. their shopping cart items) for an extended period of time (maybe 6 months or a year)? I read the user guide on CI about sessions and it says it gives you the option to set sessions to not expire by stating the session_expire_on_close variable to false in the config file, which I'm guessing keeps the session running even after the user closes the browser.

每当用户访问该网站或向他们的购物篮中添加东西时,我都可以开始会话.然后,当他们离开并决定回来时,我可以从我的数据库中的会话表中验证他们的购物车信息并将其返回给他们.

Whenever a user visits the site, or adds something to their basket, I can start the session. Then when they leave and decide to come back, I can validate their shopping cart info from the sessions table in my database and give it back to them.

我还想知道是否应该将购物车项目存储在用户指南中作为示例给出的 ci_sessions 表中的 user_data 列中?这样就够了吗?

I also wanted to know if I should store shopping cart items in the ci_sessions table given as an example in the user guide in the user_data column? Would that be sufficient?

只是想知道我是否朝着正确的方向前进.谢谢

Just want to know if I'm heading in the right direction. Thank you

推荐答案

简短回答:是的,您走在正确的道路上.

Short answer: Yes, you're on the right path.

我会怎么做(其中一些可能很明显):

How I'd do it (some of this may be obvious):

  1. session 库添加到 config/autoload.php 使其自动启动.

  1. Add the session library to config/autoload.php so it starts automatically.

根据需要在 config/config.php 中设置会话变量"部分 - sess_expiration = some long timesess_expire_on_close = falseuse_database = true

Set up the 'Session Variables' section in config/config.php as needed - sess_expiration = some long time, sess_expire_on_close = false, use_database = true etc.

为简单起见,只在会话中存储产品 ID.下面以一个基本的 add_to_cart 函数为例:

To keep it simple, only store product IDs in the session. Below is a basic add_to_cart function as an example:

public function add_to_cart($product_id) {

// Whenever a user adds an item to their cart, pull out any they already have in there

$cart_products = $this->session->userdata('cart_products');

// Add the new item

$cart_products[] = $product_id;

// And put it back into the session

$this->session->set_userdata('cart_products', $cart_products);

}

  • 使用会话中的 product_ids 从您的数据库中提取更多详细信息(例如产品名称)并将其显示在屏幕上.

  • Use the product_ids from the session to pull out more details (e.g. product name) from your db and display them on screen.

    因为 CI 保留自己的 session_id,这种方法应该在用户每次重新访问您的网站时自动重新填充会话,所以您不需要手动查询 ci_sessions 表或使用标准的 PHP 会话.

    Because CI keeps its own session_id this approach should automatically repopulate the session every time the user revisits your site, you shouldn't ever need to manually query the ci_sessions table or use standard PHP sessions.

    以上内容未经测试,但希望对您有所帮助,如有任何需要澄清的地方,请留言.

    The above isn't tested but hope it helps, shout if anything needs clarifying.

    这篇关于使用会话使用 CodeIgniter 存储购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆