是的,我知道已经问过一个类似的问题> 1000次,但是当调用header ();
后会话消失时,我遇到了一个问题。我阅读了许多解决方案,但没有一个有帮助(例如:PHP session lost after redirect),我制作了一个简单的购物篮,单击“购买”后,将商品ID输入到会话中并再次加载页面。我尝试了很多选择,但没有一个有帮助。
所有文件均采用UTF-8编码。
我不使用echo
。
session_start ();
位于php文件的开头。
index.php
<?php
session_start();
//ob_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//echo "path: ".ini_get('session.cookie_path');
//include_once $_SERVER['DOCUMENT_ROOT'] . '/test_cart/includes/magicquotes.inc.php';
$items = array(
array('id' => '1', 'desc' => 'Item 1', 'price' => 24.95),
array('id' => '2', 'desc' => 'Item 2', 'price' => 1000),
array('id' => '3', 'desc' => 'Goldfish (2 CD)', 'price' => 19.99),
array('id' => '14', 'desc' => 'JavaScript (SitePoint)', 'price' => 39.95));
if (!isset($_SESSI0N['cart'])) {
//echo "Init Cart";
$_SESSION['cart'] = array();
}
if (isset($_POST['action']) and $_POST['action'] == 'Buy') {
array_push($_SESSION['cart'], $_POST['id']);
header('Location: .');
die();
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'Claer') {
unset($_SESSION['cart']);
session_regenerate_id(true);
header('Location: .');
exit();
}
if (isset($_GET['cart'])) {
session_start();
$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
session_start();
function html($text) {
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text) {
echo html($text);
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Catalog</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border:1px solid;
}
</style>
</head>
<body>
<p>Cart:<?php
echo count($_SESSION['cart']); ?> items.</p>
<p><a href="?cart">View Cart</a></p>
<table border="l">
<thead>
<tr>
<th>Info</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>
</body>
</html>
cart.html.php
<?php
session_start();
//ob_start();
function html($text) {
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text) {
echo html($text);
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cart</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border:1px solid black;
}
</style>
</head>
<body>
<h1>My Cart</h1>
<?php if (count($cart) >0): ?>
<table>
<thead>
<tr>
<th>Info</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>Cart empty!</p>
<?php endif; ?>
<form action="?" method="post">
<p>
<a href="?">Continue shopping</a>
<input type="submit" name="action" value="Empty cart">
</p>
</form>
</body>
</html>
答案 0 :(得分:1)
您在这里至少有一个错字($ _SESSI0N而不是$ _SESSION):
// if (!isset($_SESSI0N['cart'])) { should be
if (!isset($_SESSION['cart'])) {
//echo "Init Cart";
$_SESSION['cart'] = array();
}
实际上,每次调用index.php
时,您的购物车钥匙都会重置为空数组。