首页 > 解决方案 > 从 id 来的地方重定向同一个页面

问题描述

我正在尝试,如果用户单击 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>

我想从用户在处理后在同一页面上单击添加到购物车重定向的地方做

标签: phphtmlsession

解决方案


$_SERVER['HTTP_REFERER']在您的标题功能中使用。在此处阅读更多信息:http: //php.net/manual/en/reserved.variables.server.php


推荐阅读