storing a shopping cart in a cookie?

M

Milsnips

Hi there,

i had a small project where i used a session to store the shopping cart, but
if it times out, the user loses the cart.
Then i thought instead of using session, i'll store the cart in a cookie,
but cookie only stores string values, which makes it tricky to update/remove
cart item values because i'd have to so substring, split, etc...

What i'm thinking now is to store the cart in a database table, and just add
a cookie with cartID (like a sessionID) and when the page loads, to grab the
cart details from the database and populate the page.

Any feedback appreciated to what kind of method would be best.

thanks,
Paul.
 
A

Aidy

The issue you have with storing in a database is the clean-up of old carts.
TBH I'd just store the IDs in a cookie using a comma seperated format and
Split them.
 
M

Milsnips

Hi Aidy,

Thanks for your quick reply. Yeh i also thought about the old cart data
issue, so i think it might be best to do the string split also.

regards,
Paul
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Milsnips said:
Hi there,

i had a small project where i used a session to store the shopping cart, but
if it times out, the user loses the cart.
Then i thought instead of using session, i'll store the cart in a cookie,
but cookie only stores string values, which makes it tricky to update/remove
cart item values because i'd have to so substring, split, etc...

What i'm thinking now is to store the cart in a database table, and just add
a cookie with cartID (like a sessionID) and when the page loads, to grab the
cart details from the database and populate the page.

Any feedback appreciated to what kind of method would be best.

thanks,
Paul.

One issue with cookies that you haven't mentioned is the size
limitation. The limitation differs from browser to browser, but it's
typically one or two kilobytes. If you try to add more cookies after the
limit is reached, they will just be ignored.

Also, if you store a lot of data in cookies, that data will be sent in
every request to the server.

If you want to build an application that can handle a lot of items in
the cart, you should store the cart on the server.

The database solution sounds to me to be the most stable. You have to
clean up abandoned carts after a while, but I don't see that as a big
issue. Actually it gives you the possibility to clean up old data, which
is not possible if you store it in cookies. Also the contents of the
abandoned carts might be some interresting information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top