清空 Post过程中的 cookie

cookie = new CookieContainer();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的购物车实现代码,使用 cookie 和 session 存储购物车信息: <?php session_start(); // 添加商品到购物车 function add_to_cart($product_id, $quantity) { // 如果购物车已经有该商品,则增加数量 if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id] += $quantity; } else { $_SESSION['cart'][$product_id] = $quantity; } } // 从购物车删除商品 function remove_from_cart($product_id) { unset($_SESSION['cart'][$product_id]); } // 获取购物车的商品数量 function get_cart_count() { $count = 0; if (isset($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $quantity) { $count += $quantity; } } return $count; } // 获取购物车的商品列表 function get_cart_items() { $items = array(); if (isset($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $product_id => $quantity) { $items[] = array( 'product_id' => $product_id, 'quantity' => $quantity ); } } return $items; } // 在页面头部设置购物车数量 function set_cart_count_header() { $count = get_cart_count(); header('X-Cart-Count: ' . $count); } // 添加商品到购物车 if (isset($_POST['add_to_cart'])) { $product_id = $_POST['product_id']; $quantity = $_POST['quantity']; add_to_cart($product_id, $quantity); set_cart_count_header(); } // 从购物车删除商品 if (isset($_POST['remove_from_cart'])) { $product_id = $_POST['product_id']; remove_from_cart($product_id); set_cart_count_header(); } // 显示购物车的商品列表 if (isset($_GET['view_cart'])) { $items = get_cart_items(); foreach ($items as $item) { echo 'Product ID: ' . $item['product_id'] . ', Quantity: ' . $item['quantity'] . '<br>'; } } // 清空购物车 if (isset($_GET['clear_cart'])) { unset($_SESSION['cart']); set_cart_count_header(); } ?> <form method="post"> <input type="hidden" name="product_id" value="1"> <input type="number" name="quantity" value="1"> <button type="submit" name="add_to_cart">Add to Cart</button> </form> <a href="?view_cart=1">View Cart</a> <form method="post"> <input type="hidden" name="product_id" value="1"> <button type="submit" name="remove_from_cart">Remove from Cart</button> </form> <a href="?clear_cart=1">Clear Cart</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值