簡體   English   中英

在PHP中使用會話的簡單購物車問題

[英]Simple shopping cart problem using session in PHP

我根據會話制作了簡單的PHP shoppingcart。 從博克“使用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.ini文件中(或使用session_save_path)設置的會話數據沒有有效的存儲位置,或者沒有足夠的權限來保存信息。

如果您使用的是托管公司,請確保檢查他們的Wiki,了解他們要將路徑設置到何處。 有些要求您使用的根目錄中有一個會話文件夾。

如果是localhost設置,請確保Web服務器具有對保存路徑的寫權限。

register_globals是否打開? 我看到您在會話中有一個'cart'鍵; 然后將$cart設置$cart一個空數組:

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

如果在您的php.ini中注冊了全局變量,那么執行此代碼時可能會導致購物車被覆蓋。 將其關閉或使用其他名稱。

有關register_globals的更多信息

session_start()的第一個應該放在其他任何東西之上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM