Hmm. When I hit index.php, I see that there is no existing session, which is expected. Refreshing shows me that the program has created the needed stuff. Adding an item to the cart seems to work correctly. What happens then? If I hit back, the cart seems to uninstantiate.
<pre>
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();
?>
</pre>