基本框架:
cart.php 购物车类,测试用。请自行扩展
<?php
class CART {
  var $data = 0;
  function insert() {
    $this->data++;
  }
  function delete() {
    $this->data--;
    if($this->data < 0)
      $this->data = 0;
  }
}session_start(); // 启用session
if(! isset($_SESSION["cart"]))
  $_SESSION["cart"] = new CART; // 若购物车没有创建则创建
$cart =& $_SESSION["cart"]; // 取回保存在session中的购物车
?>shop.php
<?php
require_once "cart.php";
// 根据控制信息操作购物车
if($_POST['cmd1']) {
  $cart->insert();
}
if($_POST['cmd2']) {
  $cart->delete();
}
?>
<form method=post>
<input type=text value=<?php echo $cart->data; ?>>
<input type=submit name=cmd1 value="+">
<input type=submit name=cmd2 value="-">
</form>