After adding an item to the cart, I hit the back button and my Cart object seems to uninstantiate. I\'ve made this example as concise as I can; please take a look and see if you spot anything dumb. Thanks.
application.php:
<?
require(\"cart.php\");
session_start();
session_register(\"SESSION\");
if (!isset($SESSION)) {
echo \"SESSION: no<br>\n\";
$SESSION = array();
} else {
echo \"SESSION: yes<br>\n\";
}
if (!isset($SESSION[\"cart\"])) {
echo \"SESSION[cart]: no<br>\n\";
$SESSION[\"cart\"] = new Cart;
} else {
echo \"SESSION[cart]: yes<br>\n\";
}
?>
cart.php:
<?
class Cart
{
var $items;
function Cart()
{
$this->init();
}
function init()
{
$this->items = array();
$this->total = 0;
}
function set($id, $q)
{
$this->items[$id] = $q;
}
function add($id)
{
isset($this->items[$id]) ?
$this->items[$id] += 1 :
$this->items[$id] = 1;
}
function display()
{
for ($i=0; $i<10; $i++) {
if (isset($this->items[$i])) {
printf(\"%d=%d<br>\n\", $i, $this->items[$i]);
}
}
echo \"<br>\n\";
}
}
?>
index.php:
<?
include(\"application.php\");
?>
item 1<a href=shopping_cart.php?a=A&id=1>add</a><br>
item 2<a href=shopping_cart.php?a=A&id=2>add</a><br>
item 3<a href=shopping_cart.php?a=A&id=3>add</a><br>
shopping_cart.php
<?
include(\"application.php\");
switch($a) {
case \"A\":
$SESSION[\"cart\"]->add($id);
break;
}
$SESSION[\"cart\"]->display();
?>