我是新手,我正在创建一个带购物车的网站。我有一个主登录页面,如果他们试图从这个页面登录,已经将用户重定向到主页,没有问题。但是我有另一个页面(结账页面),如果用户选择了要购买但尚未登录的项目,则将用户引导至登录页面,在从此页面登录后,它会从基因上重定向到主页。如果用户从这里登录,怎么能让它回到结账页面?
if (count($_SESSION['prodquan']) != 0)
{
print "<table border='1' align='center' width='50%'>";
print "<tr><td colspan='5' style='color:red'>";
if (isset($_SESSION['username'])) print 'Hello ' . $_SESSION['username'];
print "</td></tr>";
print "<caption style='font-size:20;color:red'>Shopping Cart</caption>";
print "<tr>";
print "<td>Product Id</td><td>Product Description</td><td>Quantity</td><td>Price</td><td>Cost</td></tr>";
foreach ($_SESSION['prodquan'] as $prod=>$quan)
{
$query = "select productdescription, price from product where productid = $prod";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
$cost = $row[1]*$quan;
print "<tr><td>$prod</td><td>$row[0]</td><td>$quan</td><td>$row[1]</td><td>$cost</td></tr>";
$_SESSION['totalcost'] += $cost;
mysqli_free_result($result);
}
print "<tr align='right'><td colspan='4'>Total Cost:</td><td>" . $_SESSION['totalcost'] . "</td></tr>";
print "</table>";
print "<table border='0' width='50%' align='center'>";
print "<tr rowspan='2'>";
print "<td align='center'><a href='checkout.php?payment=yes'>Payment</a>  <a href='validpage.php'>Products</a>  <a href='logout.php'>logout</a></td>";
print "</tr>";
print "</table>";
}
else
{
$errormessage = " The shopping cart is empty";
header("location:validpage.php?errormsg=$errormessage");
exit();
}
if (@$_GET['payment'] == "yes")
{
if (!isset($_SESSION['username']))
{
$errormessage = "Please login";
header("location:http://localhost/shoppingcart/?page_id=5?errormsg=$errormessage");
exit();
}
//$_SESSION['payment'] = "yes";
print "<td align='center'><a href='checkout.php?credit=yes'>Credit Card</a>  <a href='checkout.php?cheque=yes'>Cheque</a>  <a href='checkout.php?american=yes'>American Express</a></td>";
//unset($_GET['payment']);
}
非常感谢
答案 0 :(得分:1)
您可以使用$_SERVER['HTTP_REFERER']
来检查用户是否来自 CHECKOUT 页面
IN LOGIN PAGE,
if(!empty($_SERVER['HTTP_REFERER'])){
if($_SERVER['HTTP_REFERER']) == 'YOUR CHECKOUT URL')
//REDIRECT to CHECKOUT ie $_SERVER['HTTP_REFERER']
else
//NORMAL HOME PAGE REDIRECT
}
答案 1 :(得分:0)
只需将变量与初始重定向一起传递到登录页面,例如:
header('Location : /login?intent=/checkout/');
然后在您的登录页面上,您可以将$ _GET ['intent']存储到会话中,或将其存储为表单中的隐藏字段:
<form method="POST" action="/login">
<input type="hidden" name="intent" value="<?php echo !empty($_GET['intent']) ? htmlentities($_GET['intent']) : '/'; ?>" />
<input type="text" name="email" />
<input type="password" name="password" />
<input type="submit" value="Login" />
</form>
然后,在您的表单处理代码中:
if (!empty($_GET['intent']) {
header('Location: ' . $_GET['intent']);
}
答案 2 :(得分:0)
你们是冠军! :-)我去了CodingAnt因为代码看起来比较容易,因为我刚刚开始,但感谢你输入Jonathan。干杯