Hi,
I'm programming a shopping cart with php but i'm not sure if it's better to use session or database to save the items in the cart (cart is a class in my code). What do you advise me? Maybe the sessions are better because a guest can also put his items in his cart...
Thanks in advance... bye...^^
php shopping cart, database or session?
Started by Jacki, Oct 18 2009 07:10 AM
5 replies to this topic
#1
Posted 18 October 2009 - 07:10 AM
|
|
|
#2
Posted 18 October 2009 - 10:21 AM
I would store data in the database. Sessions will still be important, however, for managing the database access.
#3
Posted 18 October 2009 - 11:15 AM
This is my actual Cart:
class Cart
{
private $items;
public function __construct()
{
if(!isset($_SESSION['items']))
$this->items = array();
else
$this->items = $_SESSION['items'];
}
public function __destruct()
{
$_SESSION['items'] = $this->items;
}
public function add_item($id, $num)
{
if($num > 0)
$this->items[$id] += $num;
else
$this->items[$id] = 0;
}
public function show_items()
{
return $this->items;
}
}
I think that the sessions are more convenient because a guest can fill the cart while he is unregistered.. What do you think?

#4
Posted 18 October 2009 - 01:04 PM
To check out, I would still want the person to be registered.
#5
Posted 18 October 2009 - 01:06 PM
You could always make it session only if the user isn't logged in, and database-based otherwise. I assume they need to register at some point though to purchase something, at that point you can throw it into a database from the session variables.
If you're selling stuff online; it's always good to have records!
Well, that's what I would do.
If you're selling stuff online; it's always good to have records!
Well, that's what I would do.
#6
Posted 18 October 2009 - 01:10 PM
I could be wrong, but I think a properly designed database would be easier on the server than loads of session variables per user.


Sign In
Create Account


Back to top









