使用cookie和session,一下子搞定

解决方案 »

  1.   

    Cookie没用过,Session应该足够了。
      

  2.   

    购物的用cookie不太好吧,用session
      

  3.   

    应该用Session;下面是一个购物车的类自己好好研究研究吧!
    class Cart {
      var $classname = "Cart";
      var $persistent_slots = array("item", "currentItem");  var $item = array();  ## The shopping cart array($item, array("art"=>..., "num"=>..., ...))
      var $currentItem = 1; ## Next item number in cart
      
      ##
      ## Return the position and number of units 
      ## of an article in the cart(or false and 0, 
      ## if it is not in there)
      ##
      function check($art) {    if (!is_array($this->item))
          return array(false, 0);//考虑在用了unset的情况    reset($this->item);
        while(list($item, $attr) = each($this->item)) {
        
          if (isset($attr["art"]) 
           && ($attr["art"] == $art)) {
               return array($item, $attr["num"]);
          }
        }
        
        return array(false, 0);
      }  ##
      ## Delete all articles from current cart
      ##
      function reset() {    reset($this->item);
        while(list($item, $attr) = each($this->item)) {
          unset($this->item[$item]);
        }
        $this->currentItem = 1;
        
        return true;
      }  ##
      ## Add num units of an article to the cart
      ## and return the item number (or false on error).
      ##
      function add_item($art, $num) {
      
        ## Check to see if we already have some of these
        list($item, $have) = $this->check($art);
        
        ## We already have them
        if ($item) {
          $this->item[$item]["num"] += $num;
          return $item;
        }
        
        ## New article
        $item = $this->currentItem++;
        $this->item[$item]["art"] = $art;
        $this->item[$item]["num"] = $num;    return $item;
      }
      
      ##
      ## Take num units of an article from the cart
      ## and return the item number (or false on error)
      ##
      function remove_item($art, $num) {
      
        ## Check to see if we have some of these
        list($item, $have) = $this->check($art);
        
        ## Can't take them out
        if (!$item || ($num > $have)) {
          return false;
        }
        
        ## Drop the item
        if ($num == $have) {
          unset($this->item[$item]);
          return $item;
        }
        
        ## Take $num out...
        $this->item[$item]["num"] -= $num;
        return $item;
      }  ##
      ## Set quantity of an article in the cart to exactly $num
      ## and return the item number
      ##
      function set_item($art, $num) {
      
        ## Check to see if we already have some of these
        list($item, $have) = $this->check($art);
        
        ## We already have them
        if ($item) {
         if ($num > 0) {
          $this->item[$item]["num"] = $num;
         } else {
          unset($this->item[$item]);
         }
         return $item;
        }
        
        if ($num > 0) {
          ## New article
          $item = $this->currentItem++;
          $this->item[$item]["art"] = $art;
          $this->item[$item]["num"] = $num;
        }    return $item;
      }  #
      # Return the number of articles in current cart.
      #
      function num_items() {
        if (!is_array($this->item))
          return 0;    return count($this->item);
      }  function tot_arts() {
        printf("Please use \$cart->num_items() instead.\n"); ## Comment out, if you want.
        return $this->num_items();
      }  #
      # Iterator to show cart contents.
      #
      function show_all() {
        if (!is_array($this->item) or $this->num_items() == 0) {
          $this->show_empty_cart();
          return false;
        }    reset($this->item);
        $this->show_cart_open();
        while(list($item, $attr) = each($this->item)) {
          $this->show_item($attr["art"], $attr["num"]);
        }
        $this->show_cart_close();
      }  function start() {
        global $sess;
        
        $sess->register("cart");
      }  ##
      ## Dummy, to be overwritten by user.
      ##  function show_cart_open()  { 
      echo("aa");
      print_r($this->item);
        return; 
      }  function show_cart_close() { 
        return; 
      }  function show_item($art, $num) {
        printf("%s units of %s<br>\n", $num, $art);
      }
      
      function show_empty_cart() {
        printf("Your shopping cart is empty.<br>\n");
      }
    }//Test Local
    $t = new cart;
    echo($t->add_item("wogannima", 4));
    echo($t->add_item("wogannima", 4));
    echo($t->add_item("dada", 4));
    $t->set_item("wogannima", 4);
    $t->remove_item("wogannima", 5);
    $t->show_all();
    $t->show_cart_open();
      

  4.   

    用session跟踪用户操作!去看看网上的相关资料,祝你好运!