I am currently developing a simple shopping cart using PHP & HTML. I've been having troubles with using the SESSIONS in Php like:

  • Sessions not continuing when I switch pages even though I called the session_start() at the top of every page.

Is it a better approach to use sessions in keeping track of a clients cart?

My requirements are as follows:

  1. Keep track of users cart when user is switching pages.

  2. Save users cart to database when the user proceeds to check out.

  3. Clear users cart(session) when it has been idle for 30 minutes.
  • Each has its own advantages and disadvantages. Some sites use both. The question for you is that beyond the "shopping cart", what are you going to do with the data?
    – user40980
    Nov 9, 2015 at 0:57
  • Data after the shopping cart will be inserted in a table. @MichaelT
    – ellaRT
    Nov 9, 2015 at 0:58
  • 1
    What if the user abandons the cart? How long are you going to allow them to come back to the same cart? Do you allow switching of cart to and from a wishlist? Is there more data about how long between actions that you want to persist? If you are persisting it in a database, how often do you clean it out? I will stress that these are questions about the business requirements that you have. If you don't have them, you need to find out what they are. If you do have them, you need to include it in the question. If you are having trouble with the code, that is off topic here.
    – user40980
    Nov 9, 2015 at 1:01
  • I edited the question sir @MichaelT.
    – ellaRT
    Nov 9, 2015 at 1:27
  • 1
    no - i'm saying you need to find out why sessions are breaking
    – HorusKol
    Nov 9, 2015 at 4:19

2 Answers 2


Either way you will want to use sessions for persisting data through the customer experience since you're ultimately working across a stateless protocol. If I have to implement a database then I go ahead and move the sessions to the database (the client may still need a cookie depending on how you decide to go about that.) As for the cart, as soon as I had to use a database, I tended to move it to the database as well. When I'd use a framework like CodeIgniter then I'd let it make the call unless I had a compelling reason to do otherwise. Since to meet #2 you'll have to design the db to store a cart at checkout, then you may as well store it in the db across the user's experience. I'd take a look at this as well: https://stackoverflow.com/questions/12569568/shopping-cart-persistence-session-or-browser-cookie

  • Thank you for this one, this is just the justification I need for using SESSIONS.
    – ellaRT
    Nov 9, 2015 at 4:39

I don't know what your code looks like, here is mine and it works:

  $_SESSION['name'] = 'Bee';
<a href="page2.php">next</a>

  echo 'Hello, '. $_SESSION['name'];

And I would rather use sessions, cause I think it's more simple to process the business tasks

Not the answer you're looking for? Browse other questions tagged or ask your own question.