Forum Moderators: buckworks

Message Too Old, No Replies

Shopping basket strategy

Which is better cookies or database?

         

gman

10:23 am on Apr 2, 2004 (gmt 0)

10+ Year Member



I am currently working on a PHP/MySQL based ecommerce engine and was wondering about the best strategy to use for remembering the customers shopping basket.

I the past i have worked on baskets that get stored in a db table - however this table invarably grows over time and needs some kind of cleaning out script / cron thing.

I was thinking about just storing the basket as an array in a cookie (product id & quantity). Does anyone use this technique? Would i be limited by the number of items i can add?

Any thoughts appreciated!

martyt

2:57 pm on Apr 2, 2004 (gmt 0)

10+ Year Member



If you use a cookie, you're passing *all* that data back and forth on every page hit. Not a good idea, especially if the cart will get very big or if you're passing a lot of data. You might be able to get away with it if you're compact about what data you need in the cart -- all you should really require is the productid and quantity for each item in the cart.

The database solution is, imo, the cleanest. At order creation time, you'd transfer the data from the shopping cart table to order table and delete it from the shopping cart table, so the only growth you'd see in the shopping cart table would be from abandoned carts. And you can take care of that every now and then by deleting all rows that haven't been touched "recently." (keep a timestamp on every item in the cart so you can identify those that are old and not a customer in progress.)

With the database solution, the only cookie you need to pass back and forth is the cart ID (I'd use a GUID to ensure uniqueness). Store the cart ID in a session variable and you don't even have to pass it around as a cookie (though you run the risk of customers losing their cart unexpectedly if the server restarts your web app).