shopping cart for non registered users

php, session, shopping-cart

Solution

Tracking a user. Use a GUID encoded into a cookie with an nYear expiry.

Storing a Shopping Bag. You don't want to store the bag in the cookie, mainly due its possible size. This leaves the option of persiting it to a medium and retrieving it from the medium. Using anything except a database for this would be like going back in time, databases excel at storing and retrieving data.

Managing you Shopping Bag. Now, the question of your schema, firstly, if your going to be running queries against the shopping bags in the database (i.e. how many shopping bags contain item x) you probably want a traditional relational schema. However this has overheads in terms of inserting. updating, selecting (and joining) and deleting bag data (at some point you'll have bags that will never be used again but are taking up precious disk space). With a busy site, this is a fair few Traranactions Per Second, but any database should be able to cope. If you don't need to query the shopping bags in the database, then you can store it as XML. Just serialize the bag and dump it into a table, with the PK as the GUID as stored in the users cookie. This would be a lot faster than a traditional schema, plus you could always tear apart the XML in the future if a requirment did come up for a relational schema.

This is what we do (Xml Bag), and we have a million+ customer base.

Problem

I am in the process of making a website that involves a shopping cart. There are two major requirements: The user experience guys want login/authentication to be the very last step in the entire work flow. The user gets to do all the shopping and is asked to login only at the time of checking out. The shopping cart shouldn't expire(not even on browser close) unless the user (registered or not) does check-out. In the above context, I have the following question with respect to maintaining the cart's state: Should I go with file based or database sessions? Please keep in mind this would be for unregistered users. My apprehension is that I'll end up having lots of records in the database. Another option seems to be to put the cart contents in an encrypted cookie, but then there's a size limitation on the cookie file. What would you do in this case ? I would really appreciate your answers.

Original source