我正在尝试,如果用户单击shop-male.php中的添加到购物车按钮,它将转到add_cart.php,在处理完成后,它将再次重定向shop-male.php,但是如果用户单击shop-single.php,该怎么办?我想在shop-single.php上经过重定向后 我们更了解我的代码在这里
shop-male.php
<a href="add_cart?id=<?php echo $row['id']; ?>" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus"></span> Cart</a>
add_cart.php
<?php
session_start();
//check if product is already in the cart
if(!in_array($_GET['id'], $_SESSION['cart'])){
array_push($_SESSION['cart'], $_GET['id']);
$_SESSION['message'] = 'Product added to cart';
}
else{
$_SESSION['message'] = 'Product already in cart';
}
header('location: shop-male');
?>
另一个页面是shop-single.php
<a href="add_cart?id=<?php echo $row['id']; ?>"> <button class="btn btn-primary"><i class="icon-bag"></i> Add to Cart</button> </a>
我想在用户点击“添加到购物车”后在同一页面上执行重定向操作
答案 0 :(得分:0)
在标题函数中使用$_SERVER['HTTP_REFERER']
。在此处详细了解:http://php.net/manual/en/reserved.variables.server.php
答案 1 :(得分:0)
尝试
header('Location: ' . $_SERVER['HTTP_REFERER']);
答案 2 :(得分:0)
尝试以下代码在GET值中分配重定向页面
shop-male.php
<a href="add_cart?id=<?php echo $row['id']; ?>&redirect=shop" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-plus"></span> Cart</a>
add_cart.php
<?php
session_start();
//check if product is already in the cart
if(!in_array($_GET['id'], $_SESSION['cart'])){
array_push($_SESSION['cart'], $_GET['id']);
$_SESSION['message'] = 'Product added to cart';
}
else{
$_SESSION['message'] = 'Product already in cart';
}
if($_GET['redirect']=='single')
{
header('location: shop-single');
}
else
{
header('location: shop-male');
}
?>
shop-single.php
<a href="add_cart?id=<?php echo $row['id']; ?>&redirect=single"> <button class="btn btn-primary"><i class="icon-bag"></i> Add to Cart</button> </a>
答案 3 :(得分:0)
您可以检查以下内容:
if (isset($_SERVER["HTTP_REFERER"])) {
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
或
您必须重定向具有当前产品ID的男性商店页面。您可以使用以下代码:
$host = $_SERVER['HTTP_HOST'] . '/';
$project = explode('/', $_SERVER['REQUEST_URI'])[1];
header('location: $host.$project.'/shop-male?'.$_GET['id']);