【问题标题】:Simple shopping cart problem using session in PHP在 PHP 中使用会话的简单购物车问题
【发布时间】:2010-01-08 13:29:52
【问题描述】:

我基于会话制作了简单的 PHP 购物车。来自博克“使用 php & Mysql 构建您自己的数据库驱动的网站”

它包含3个文件:控制器index.php,以及两个模板文件catalog.html.phpcart.html.php >

问题是当我点击“查看您的购物车”链接时,会话将结束并且 $_session['cart'] 将自动取消设置或其他东西

对不起,我不能解释清楚 所以这里是代码:

/* index.php - 控制器*/

$items = array(
 array('id' => '1', 'desc' => 'Candian-Australian Dictionary',
  'price' => 24.95),
 array('id' => '2', 'desc' => 'As-new parachute (never opened)',
  'price' => 1000),
 array('id' => '3', 'desc' => 'Songs of the Goldfish (2CD set)',
  'price' => 19.99),
 array('id' => '4', 'desc' => 'Simply JavaScript (SitePoint)',
  'price' => 39.95));

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

if (isset($_POST['action']) and $_POST['action'] == 'Buy')
{
 // Add item to the end of the $_SESSION['cart'] array
 $_SESSION['cart'][] = $_POST['id'];
 header('Location: .');
 exit();
}


if (isset($_POST['action']) and $_POST['action'] == 'Empty cart')
{
 // Empty the $_SESSION['cart'] array
 unset($_SESSION['cart']);
 header('Location: ?cart');
 exit();
}

if (isset($_GET['cart']))
{
 $cart = array();
 $total = 0;
 foreach ($_SESSION['cart'] as $id)
 {
  foreach ($items as $product)
  {
   if ($product['id'] == $id)
   {
    $cart[] = $product;
    $total += $product['price'];
    break;
   }
  }
 }
 include 'cart.html.php';
 exit();
}

include 'catalog.html.php';
?>

/* catalog.html.php - 显示所有产品*/

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
 '/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Product catalog</title>
<meta http-equiv="content-type"
content="text/html; charest=utf-8"/>
<style type="text/css">
table {
 border-collapse: collapse;
}
td, th {
 border: 1px solid black;
}
</style>
</head>
<body>
<p>Your shopping cart contains <?php
 echo count($_SESSION['cart']); ?> items.</p>
<p><a href="?cart">View your cart</a></p>
<table border="1">
 <thead>
  <tr>
   <th>Item Description</th>
   <th>Price</th>
  </tr>
 </thead>
 <tbody>
  <?php foreach ($items as $item): ?>
   <tr>
    <td><?php htmlout($item['desc']); ?></td>
    <td>
     $<?php echo number_format($item['price'], 2); ?>
    </td>
    <td>
     <form action="" method="post">
      <div>
       <input type="hidden" name="id" value="<?php
        htmlout($item['id']); ?>"/>
       <input type="submit" name="action" value="Buy"/>
      </div>
     </form>
    </td>
   </tr>
  <?php endforeach; ?>
 </tbody>
</table>
<p>All prices are in imaginary dollars.</p>
</body>
</html>

/* cart.html.php - 显示购物车中的产品*/

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
 '/includes/helpers.inc.php'; ?>
<!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Shopping cart</title>
<meta http-equiv="content-type"
content="text/html; charest=utf-8"/>
<style type="text/css">
table {
 border-collapse: collapse;
}
td, th {
 border: 1px solid black;
}
</style>
</head>
<body>
<h1>Your Shopping Cart</h1>
<?php if (count($cart) > 0): ?>
<table>
 <thead>
  <tr>
   <th>Item Description</th>
   <th>Price</th>
  </tr>
 </thead>
 <tfoot>
  <tr>
   <td>Total:</td>
   <td>$<?php echo number_format($total, 2); ?></td>
  </tr>
 </tfoot>
 <tbody>
  <?php foreach ($cart as $item): ?>
   <tr>
    <td><?php htmlout($item['desc']); ?></td>
    <td>
     $<?php echo number_format($item['price'], 2); ?>
    </td>
   </tr>
  <?php endforeach; ?>
 </tbody>
</table>
<?php else: ?>
<p>Your cart is empty!</p>
<?php endif; ?>
<form action="?" method="post">
 <p>
  <a href="?">Continue shopping</a> or
  <input type="submit" name="action" value="Empty cart"/>
 </p>
</form>
</body>
</html>

提前致谢

【问题讨论】:

    标签: php session


    【解决方案1】:

    这里的典型问题是 php.ini 文件(或 session_save_path)中没有设置会话数据存储的有效位置,或者没有足够的权限来保存信息。

    如果您使用的是托管公司,请务必查看他们的 wiki,了解他们希望将路径设置到的位置。有些要求您在使用的根目录中有一个会话文件夹。

    如果是 localhost 设置,请确保 Web 服务器对保存路径具有写入权限。

    【讨论】:

      【解决方案2】:

      register_globals 开启了吗?我看到您的会话中有一个'cart' 密钥;然后将$cart 设置为一个空数组:

      if (isset($_GET['cart']))
      {
       $cart = array();
       $total = 0;
       foreach ($_SESSION['cart'] as $id)
       {
         ...
      

      如果在您的 php.ini 中注册了全局变量,这可能会导致您的购物车在执行此代码时被覆盖。要么关闭它,要么使用不同的名称。

      更多关于register_globals的信息

      【讨论】:

        【解决方案3】:

        session_start() 的第一个应该在其他任何事情之前排在最前面。

        【讨论】:

        • session_start() 已经出现在上述代码的任何输出之前。
        • 但将其放在首位仍然是一种好习惯:-),并且没有人要求他检查他在购物车中添加商品的页面,如果会话正常或至少有什么问题在会话中添加...
        • @Bogdan 然而,这只是一个评论,而不是问题的答案。
        猜你喜欢
        • 1970-01-01
        • 2015-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-11
        • 2019-09-17
        • 2023-03-27
        相关资源
        最近更新 更多