我根据会话制作了简单的PHP购物车。来自bock“使用php& Mysql构建您自己的数据库驱动的网站”
它包含3个文件:控制器 index.php ,以及两个模板文件 catalog.html.php , cart.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>
提前致谢
答案 0 :(得分:0)
这里的典型问题是在php.ini文件中(或使用session_save_path)没有有效存储会话数据的位置,或者没有足够的权限来保存信息。
如果您使用的是托管公司,请务必检查他们的wiki,了解他们希望路径设置的位置。有些要求您在根目录中使用会话文件夹。
如果是localhost设置,请确保Web服务器具有对保存路径的写入权限。
答案 1 :(得分:0)
register_globals
开启了吗?我看到你的会话中有'cart'
个密钥;然后将$cart
设置为空数组:
if (isset($_GET['cart']))
{
$cart = array();
$total = 0;
foreach ($_SESSION['cart'] as $id)
{
...
如果你的php.ini中注册了globals,这可能会导致你的购物车在执行此代码时被覆盖。关闭它或使用不同的名称。
的更多信息答案 2 :(得分:-1)
session_start()的第一个应该先于其他任何内容。