Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

unset shopping cart session

Is it sufficient to use unset to clear the cart contents after a successful transaction or should I be doing more than this?

unset($_SESSION['cart_array']);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now that I read Dave's comment, I am beginning to think that this is one of those questions with "simple" answers, but with a HUGE subtext about application design.  As he wisely points out, "a lot of additional code and database info..."  A session and a shopping cart should always be different things.  There are books written about the designs here (there are even college degrees about the designs) because getting this right is the underpinning of Amazon.  Getting it wrong is the underpinning of pain, frustration, tears, and bankruptcy.  It's worth studying this issue and all of its attendant issues very, very carefully.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

Thanks for all the comments. Yes, for now it's only a session based cart. I am doing that simply to get the hang of creating a basic cart. I understand that I need to design it better by using cookies and a database.  I will get there once I at least have a simple session based cart working.

Julain is correct in saying that I am not trying to do anything large scale or complicated. It's a really simple cart, there is no shipping, no taxes, no sizes or colors etc.

The whole cart is stored in a session array and I just want to empty it out once the order is complete without destroying all sessions to the extent that it destroys the user's session and they are logged out. I just want the cart session emptied out.
@ Julian,

not sure if you saw but I replied to your post regarding this question:

https://www.experts-exchange.com/questions/28992764/simple-shopping-cart-database-structure.html
I would still go with a cookie approach over sessions - even for simple sites. The coding is not that complicated.

If you are dead set on sessions then what you have at the moment should be sufficient to clear the cart.

You could also just do this
$_SESSION['cart_array'] = array();

Open in new window

If you're using the PHP session for a shopping cart, you're doing it wrong, full stop.  And this question seems to focus on an implementation detail without understanding the larger view of the system.  It would be really helpful to fly up to 50,000 feet and look down on the bigger picture for a moment.  

To the point, what is a shopping cart?  At the object-oriented design level a shopping cart can be thought of as a set of interfaces that allow clients and servers to interact in predictable and known ways.  You can get fully-configured, working, open-source shopping carts and take them apart to learn how others have done this.  Please give yourself plenty of time - this is pretty much what the 1990's was all about in the intersection of retail and computer science.  

You can set out to write your own task-element statements, something like this:

1. User may add an item to the cart
2. User may remove an item from the cart
3. Cart will remember user and items
4. User can buy an item from the cart
5. User can buy all items in the cart
6. User can log-in and become authenticated
7. User can view a shopping cart
8. User can empty a shopping cart
9. Etc, etc, etc

Once you know what you want the cart to do, you can start writing the Interface(s) that define the methods you will use.  This application design process would precede any of the coding - design is the step you take to get your head around the breadth of the problem.  Use CRC cards, and your database design will follow easily.  It's only after you've designed the application that you need to think about the implementation details, such as "how do I write code for each of these task elements?"

You may find some useful information in college courses, too.  It's not clear to me that this is 100% up-to-date, but it's a starting point.
http://www.cs.colostate.edu/~mstrout/WebClass/program2.html
Thanks again to all. I just have one last thing to say about this and then I will close it down. I just want to mention it because it sounds like it is better practice to use cookies and a database rather than sessions. Unless it works differently to a "remember me" function with cookies, I just logged into EE using Google chrome and checked the "remember me" function. And as expected when I logged in and then closed the browser, upon opening my browser and going to the Experts Exchange website, I was still logged in. Totally behaved in the expected way.

However, being a bit security conscious and somewhat paranoid, I do all my browsing in an incognito window ( I can't be the only one). Now when I check "remember me", login and then close the browser, I have to log in again when I go back to the website. So, that fact that I checked "remember me" did not actually get the website to remember me because I had to log in again.

So, wouldn't this happen on an e-commerce site in incognito mode? If I closed the browser I would STILL go back to an empty cart whether the site used cookies or not.

Don't get me wrong, I am not trying to give this as a reason not to go this route, I am just asking if there is a way around it or not?
wouldn't this happen on an e-commerce site in incognito mode?
Yes.  When you're in private browsing or incognito mode the browser will dispose of all history and cookies as soon as it is closed.  Here is what Firefox says.  Might be worth checking into these features in a little more detail.
User generated image
It seems that a user would have to go into their browser settings and manually allow cookies only or cookies from a particular website. It would be interesting to know what percentage of users actually use private browsing. It might give an insight as to how much cookie based functionality you should or shouldn't use.
If you block cookies, you can't use most shopping carts.  Most people don't turn cookies off anyway.  Some may use 'private' browsing but that still allows them to shop until they close the 'private' window.
Yeah, I was just thinking about the point that I should use a shopping cart with cookies instead of sessions. It makes sense because if you close the browser you can go back and your cart is still there. But if you load your cart in an incognito window, close the browser and go back again, the contents of your cart will be gone, even though you are using a cookie based cart. Well, I think that is what would happen anyway, based on the little "remember me" exercise I did.
If you are using sessions then you are also using cookies.  At least that is the most common method.  The other way is to show the PHP session ID in the address bar and most of us don't want to do that.
Here is how PHP sessions work.  Note the information about cookies.
https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html