2013-08-27 50 views
0

大家好我正在開發一個沒有登錄和註銷選項的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; 

}

+0

關閉瀏覽器通常會結束會話,所以其他事情正在進行。您可能需要張貼一些代碼... –

+0

david請檢查 – user2362946

回答

0

我不知道這一點,但我認爲會話變量存儲在cookie中。這可能是關閉瀏覽器後會話仍然存在的原因。請點擊this鏈接。你可以用session_unset()

0

釋放你的會話變量只需使用session_destroy(); 這必將破壞所有存儲會話

,你可以使用它像這樣

if(isset($_POST['destroy_btn'])) 
    { 
    session_destroy(); 
    header('location: homepage.php'); 
    //redirect or refresh it after destroying the sessions 
    } 

而且也,瀏覽器有一個選項叫做會話還原,當您重新打開瀏覽器時恢復上次會話。只需關閉此選項。

+0

只有當選項卡關閉或瀏覽器關閉時,我才必須調用此session_destroy()。 – user2362946

+0

啊。我認爲你必須關閉瀏覽器的「會話恢復」功能。嘗試一下。 –