Handbags at dawn.
🙂
I think I was in super stress mode or something.
Well, let's see. More details.
During the testing phase the web site works fine. The cart does what it says on the tin.
The final Production site is rather unstable. The rest of the site, pulling stuff from the database and displaying it, is great, it's just the cart that's unstable. When you add something to the cart, it adds ok. If you go looking for another item, the first item might be there when you return to the cart, and maybe not.
I have session_start() at the top of each page, each page being a .php page (there are no .html pages). I also call the cart functions before the session starts.
Here's some code:
// Add cart tools
include("r/wfcart.php");
// Start the session
session_start();
session_regenerate_id();
// Initalise the cart
$cart =& $_SESSION['cart'];
if(!is_object($cart)) $cart = new wfCart();
Running phpinfo() on each server gives the same results. There are a couple of differences, but these seem to be unconnected to the sessions, timeouts or anything like that.
I have tried to regenerate the php session id, see code, and this doesn't really do too much. I've also upped the session.gc_maxlifetime time to 3000, in a vane attempt to crack it.
Here's wfcart:
<?php
/*
######################################################################
# __ __ __ ___ #
#/\ \ __/\ \ /\ \ /'___\ #
#\ \ \/\ \ \ \ __\ \ \____/\ \__/ ___ _ __ ___ __ #
# \ \ \ \ \ \ \ /'__`\ \ '__`\ \ ,__\/ __`\/\`'__\/'___\ /'__`\ #
# \ \ \_/ \_\ \/\ __/\ \ \L\ \ \ \_/\ \L\ \ \ \//\ \__//\ __/ #
# \ `\___x___/\ \____\\ \_,__/\ \_\\ \____/\ \_\\ \____\ \____\ #
# '\/__//__/ \/____/ \/___/ \/_/ \/___/ \/_/ \/____/\/____/ #
# #
# ) ___ #
# (__/_____) webforce cart v.1.2 #
# / _ __ _/_ (c) 2004 Eaden McKee #
# / (_(_/ (_(__ webforce.co.nz/cart #
# (______) all rights reserved #
# #
# Session based, Object Oriented Shopping Cart Component for PHP #
# #
######################################################################
# Ver 1.4 - demo included
# Ver 1.3 - bugfix with total
# Ver 1.2 - added empty_cart()
# Ver 1.0 - initial release
You are allowed to use this script in websites you create.
However you may not distribute any part of this script.
*** Instructions at [url]http://www.webforce.co.nz/cart/php-cart.php[/url] ***
**** READ THEM! ***
BUGS/PATCHES
Please email [email]eaden@webforce.co.nz[/email] with any bugs/fixes/patches/comments etc.
See [url]http://www.webforce.co.nz/cart/[/url] for updates to this script
*/
class wfCart {
var $total = 0;
var $itemcount = 0;
var $items = array();
var $itemprices = array();
var $itemqtys = array();
var $iteminfo = array();
function cart() {} // constructor function
function get_contents()
{ // gets cart contents
$items = array();
foreach($this->items as $tmp_item)
{
$item = FALSE;
$item['id'] = $tmp_item;
$item['qty'] = $this->itemqtys[$tmp_item];
$item['price'] = $this->itemprices[$tmp_item];
$item['info'] = $this->iteminfo[$tmp_item];
$item['subtotal'] = $item['qty'] * $item['price'];
$items[] = $item;
}
return $items;
} // end of get_contents
function add_item($itemid,$qty=1,$price = FALSE, $info = FALSE)
{ // adds an item to cart
if(!$price)
{
$price = wf_get_price($itemid,$qty);
}
if(!$info)
{
$info = wf_get_info($itemid);
}
if($this->itemqtys[$itemid] > 0)
{ // the item is already in the cart..
// so we'll just increase the quantity
$this->itemqtys[$itemid] = $qty + $this->itemqtys[$itemid];
$this->_update_total();
} else {
$this->items[]=$itemid;
$this->itemqtys[$itemid] = $qty;
$this->itemprices[$itemid] = $price;
$this->iteminfo[$itemid] = $info;
}
$this->_update_total();
} // end of add_item
function edit_item($itemid,$qty)
{ // changes an items quantity
if($qty < 1) {
$this->del_item($itemid);
} else {
$this->itemqtys[$itemid] = $qty;
// uncomment this line if using
// the wf_get_price function
// $this->itemprices[$itemid] = wf_get_price($itemid,$qty);
}
$this->_update_total();
} // end of edit_item
function del_item($itemid)
{ // removes an item from cart
$ti = array();
$this->itemqtys[$itemid] = 0;
foreach($this->items as $item)
{
if($item != $itemid)
{
$ti[] = $item;
}
}
$this->items = $ti;
$this->_update_total();
} //end of del_item
function empty_cart()
{ // empties / resets the cart
$this->total = 0;
$this->itemcount = 0;
$this->items = array();
$this->itemprices = array();
$this->itemqtys = array();
$this->itemdescs = array();
} // end of empty cart
function _update_total()
{ // internal function to update the total in the cart
$this->itemcount = 0;
$this->total = 0;
if(sizeof($this->items > 0))
{
foreach($this->items as $item) {
$this->total = $this->total + ($this->itemprices[$item] * $this->itemqtys[$item]);
$this->itemcount++;
}
}
} // end of update_total
}
?>
OK - Questions:
Should I store my session id in a cookie, and reference that at the top of each page?
Will that make the session more stable?
Should I use cookies to store the items and prices?
Any help would be brill.
🙂