1 Replies - 229 Views - Last Post: 03 August 2019 - 01:35 AM Rate Topic: -----

#1 trix1210   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-August 19

cookie vs sessions for storing users id for ecommerce site

Posted 03 August 2019 - 01:06 AM

I have an ecommerce shop online using php, sql, javascript,ajax and sessions.
I have both guest and members cart options at checkout.
Everything works fine.
I store my cart items in a session currently.
Users can log in or have a guest cart.
Guests cart userids are referenced by the current session id.
members can login and their carts are referenced by their usersids from the database.
The problem is, the session expires after a certain amount of time and so the cart items are lost and the user has to start again.
On doing some research I have found that after the user logs in, I can store his user id in a cookie and I can specify how long that cookie lasts for which is ideal!
I am thinking of changing the code so that I store the items added to the cart in my database tables and simply reference them with the user id ive stored in his cookie.
That way He can shop for ages and not lose his cart and I can send abandon cart emails etc...
I think this would work well as nearly every website uses cookies so people have to have them enabled in their browser these days. I could show a warning message if cookies arent enabled anyway..
What does everyone think about this?
Please note I am not seeking security advice here.

Is This A Good Question/Topic? 0
  • +

Replies To: cookie vs sessions for storing users id for ecommerce site

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7492
  • View blog
  • Posts: 15,527
  • Joined: 16-October 07

Re: cookie vs sessions for storing users id for ecommerce site

Posted 03 August 2019 - 01:35 AM

View Posttrix1210, on 03 August 2019 - 03:06 AM, said:

I am thinking of changing the code so that I store the items added to the cart in my database tables

You should be doing this regardless.

PHP Sessions use cookies, unless you're going out of your way to avoid them. Rather, a PHP session also has the logic of aging out, so while the underlying cookie needn't age out, the server object instantiated for the session can be disposed of after a time.

I believe session_id really is the cookie value, hence the idea of session_start potentially being a restart. With this in mind, if you store the data you're currently storing in a session to the database referenced by a session_id, you could persist a cart even if you don't implement a user login system.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1