我正在写一个有3页的程序。
在第1页,用户可以选择他想要购买的早餐产品数量。在选择数量后,用户点击提交按钮,如果用户未注册,则需要他进行注册。如果用户已注册,则会将他们引导至第3页。
但是,如果用户首先进入第2页并且没有从第1页中选择的数量,那么在他注册并按下提交后,它将重定向到第1页,然后一旦他们选择了数量并点击提交页面1它将转到第3页。
我很难在页面之间维护会话变量,因为如果用户返回该页面,其中有两个表单会被覆盖。
第1页:
<?php
session_start();
$_SESSION['name']= $_POST['name'];
$_SESSION['email']= $_POST['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Product Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a class="active" href="product.php">Product</a></li>
<li><a href="registration.php">Registration</a></li>
<li><a href="invoice.php">Invoice</a></li>
<li style="float:right"><a href="registration.php">Login</a></li>
</ul>
<?php
$action = '';
if (!empty($_SESSION['name']) or !empty($_SESSION['email'])) {
$action = "invoice.php";
}
else {
$action = "registration.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
<div class="container">
<img src="images/platter.jpg" alt="Breakfast Platter" style="float: left; width: 400px; height: 300px;";>
<h1>Breakfast Platter</p>
<p>The breakfast platter option comes with two fried eggs, four pancakes, and a bunch of bacon.</p>
Quantity: <input type="number" name="platter_quantity" min="0">
<p value="10.99" name="platter_price">Price: $10.99</p>
</div>
<div class="container">
<img src="images/yogurt.jpg" alt="Yogurt Parfait" style="float: left; width: 400px; height: 300px;">
<h1>Yogurt Parfait</p>
<p>The yogurt parfait option comes with two cups of yogurt, oats, and a mixture of berries.</p>
Quantity: <input type="number" name="yogurt_quantity" min="0">
<p value="6.99" name="yogurt_price">Price: $6.99</p>
</div>
<div class="container">
<img src="images/waffles.jpg" alt="Waffles" style="float: left; width: 400px; height: 300px;";>
<h1>Waffles</p>
<p>The waffles option comes with two buttermilk waffles with butter and syrup.</p>
Quantity: <input type="number" name="waffles_quantity" min="0">
<p value="$4.99" name="waffles_price">Price: $4.99</p>
</div>
<br>
<button class="button" type="submit" name="submit">Submit</button>
</form>
</body>
</html>
第2页:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$_SESSION['platter_quantity'] = $_POST['platter_quantity'];
$_SESSION['yogurt_quantity'] = $_POST['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $_POST['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a href="product.php">Product</a></li>
<li><a class="active" href="registration.php">Registration</a></li>
<li><a href="invoice.php">Invoice</a></li>
<li style="float:right"><a href="registration.php">Login</a></li>
</ul>
<br>
<?php
$action = '';
if (!empty($_SESSION['platter_quantity']) or !empty($_SESSION['yogurt_quantity']) or !empty($_SESSION['waffles_quantity'])) {
$action = "invoice.php";
}
else {
$action = "product.php";
}
?>
<form action="<?php echo $action; ?>" method="post">
Name: <input type="text" name="name" pattern="[A-Za-z]" required><br><br>
E-mail: <input type="text" name="email" pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required><br><br>
<input type="submit">
</form>
<br>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $_SESSION["platter_quantity"] . "<br>";
echo "Yogurt: " . $_SESSION["yogurt_quantity"] . "<br>";
echo "Waffles: " . $_SESSION["waffles_quantity"];
?>
</body>
</html>
第3页:
<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
?>
<!DOCTYPE html>
<head>
<title>Invoice Page</title>
<link rel="stylesheet" type"text/css" href="settings.css">
</head>
<html>
<body>
<ul>
<li><a href="product.php">Product</a></li>
<li><a href="registration.php">Registration</a></li>
<li><a class="active" href="invoice.php">Invoice</a></li>
<li style="float:right"><a href="registration.php">Login</a></li>
</ul>
<h1>Hi! Welcome <?php echo $_SESSION['name']; ?>! </h1>
<?php
print_r($_SESSION);
echo "<br>Platter: " . $platter_quantity . "<br>";
echo "Yogurt: " . $yogurt_quantity . "<br>";
echo "Waffles: " . $waffles_quantity;
?>
</body>
</html>
在不使用数据库的情况下,使用会话变量实现此目的的最佳方法是什么?
我也试过这样做,但似乎没有用:
<?php
session_start();
if (empty($_SESSION['name']) or empty($_SESSION['email'])) {
$_SESSION['name'] = $POST_['name'];
$_SESSION['email'] = $POST_['email'];
}
else {
$name = $_SESSION['name'];
$name = $_SESSION['email'];
}
if (empty($_SESSION['platter_quantity']) or empty($_SESSION['yogurt_quantity']) or ($_SESSION['waffles_quantity'])) {
$_SESSION['platter_quantity'] = $POST_['platter_quantity'];
$_SESSION['yogurt_quantity'] = $POST_['yogurt_quantity'];
$_SESSION['waffles_quantity'] = $POST_['waffles_quantity'];
}
else {
$platter_quantity = $_SESSION['platter_quantity'];
$yogurt_quantity = $_SESSION['yogurt_quantity'];
$waffles_quantity = $_SESSION['waffles_quantity'];
}
?>
答案 0 :(得分:2)
你永远不会先插入$ _POST或$ _GET而不先检查它们是否设置isset($_POST['variable'])
,你也可以在这里使用检查 - 检查是否存在$ _POST变量,如果它们存在,使用它们,如果没有,则分配$ _SESSION变量。所以在第3页,你会有类似的东西:
$name = $_SESSION['name'] = (isset($_POST['name']) ? $_POST['name'] : ((isset($_SESSION['name']) ? $_SESSION['name'] : '')));
等等其他变量。这样做是检查$ _POST,如果设置了它,它会更新$ _SESSION变量,如果没有设置,它只是用已经存在的$ _SESSION更新$ _SESSION变量 - 变量,如果它也不存在,它会将变量$name
和$_SESSION['name']
都设置为空,然后您可以在以后检查脚本(并重定向等)。