【发布时间】:2013-08-27 07:26:12
【问题描述】:
大家好,我正在开发一个没有登录和注销选项的 Web 应用程序。这是一个电子商务网站。因此,为了坚持订购不同的产品,我将订单数据保存在会话中。但即使在我关闭浏览器或标签后,会话也不会破坏。我没有明确销毁,所以在这些情况下如何销毁会话。
请提供一些见解。 PHP 开发新手。
session_start();
function addtocart($productid,$quantity,$amount){
if($productid === "" or $quantity<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($productid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$productid;
$_SESSION['cart'][$max]['quantity']=$quantity;
$_SESSION['cart'][$max]['amount'] =$amount;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$productid;
$_SESSION['cart'][0]['quantity']=$quantity;
$_SESSION['cart'][0]['amount']=$amount;
}
}
function product_exists($pid){
$max=count($_SESSION['cart']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid === $_SESSION['cart'][$i]['productid']){
$flag=1;
break;
}
}
return $flag;
}
【问题讨论】:
-
关闭浏览器通常会结束会话,所以会发生其他事情。您可能需要发布一些代码...
标签: php session shopping-cart