2015-02-24 102 views
-1

会话存储阵列似乎只记住一个键值对。当我在浏览器中按下后退按钮时,数组似乎是空的。我究竟做错了什么? 下面的代码: 感谢您的帮助

<?php 
 
if(!isset($_SESSION)) { 
 
    session_start(); 
 
     $_SESSION['cart']=array(); 
 
} 
 

 
?> 
 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <title>Shoppy</title> 
 
</head> 
 
<body> 
 
<div><a href="index.php">back to store</a></div> 
 

 
\t \t \t <?php include 'products.inc.php'; ?> 
 
\t \t \t <?php $p=$_GET["product"] ?> 
 
\t \t \t <span><?php echo $products[$p]['name']; ?> </span><?php echo $products[$p]['price']; ?> € 
 
\t \t \t <div> 
 
\t \t \t \t <img src="<?php echo $products[$p]['picture']; ?>" alt=""> 
 
\t \t \t </div> 
 
\t \t \t 
 
\t 
 
\t 
 
\t <form action="" method="POST"> 
 
\t \t <input type="hidden" name="form" value=<?php echo $p ?>> 
 
\t \t <button type="submit">buy</button> 
 
\t </form> 
 
\t <?php if (!empty ($_POST) && isset($_POST)) 
 
{ 
 
\t array_push($_SESSION['cart'], $_POST['form']); 
 
\t 
 

 
\t } ?> 
 
\t <?php include 'cart.inc.php'; ?> 
 
</body> 
 
</html>

+0

也许是因为你清除车在每一页上的负载?(4号线) – Halcyon 2015-02-24 00:15:47

回答

0

变化

if(!isset($_SESSION)) { 
    session_start(); 
     $_SESSION['cart']=array(); 
} 

session_start(); 
if(!isset($_SESSION['cart'])) { 
     $_SESSION['cart']=array(); 
} 

你需要调用session_start() befor试图在会话中做任何事情。此外,当您检查它是否设置,检查会话变量(即isset($_SESSION['someindex']),不是会议本身(即isset($_SESSION))。

+0

呀,这似乎主要工作,但我的购物车不会更新,除非我推动购买。这是我的购物车: – 2015-02-24 00:33:10

+0

\t

Your shopping cart contains:
\t \t
    \t \t
  • \t \t \t € \t \t \t \t \t \t \t \t
  • \t
\t
Your shopping cart is empty
2015-02-24 00:33:33

+0

@BartMertens它返回一个缓存页面(即旧数据)的正常返回按钮。 '。 – developerwjk 2015-02-24 00:36:25