简体   繁体   中英

redirect that same page from where id is coming

i am trying that if user click on add to cart button from shop-male.php it goes to add_cart.php, after process complete it redirect again shop-male.php but what if user click from shop-single.php and i want redirect after process on shop-single.php our better understand my code is here

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');

?>

and another page is 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>

i want to do from where user click add to cart redirect on that same page after process

Use $_SERVER['HTTP_REFERER'] in your header function. Read more about it here: http://php.net/manual/en/reserved.variables.server.php

尝试

header('Location: ' . $_SERVER['HTTP_REFERER']);

Try this below code assign redirect page in GET value

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>

You can check with this:

if (isset($_SERVER["HTTP_REFERER"])) {
        header("Location: " . $_SERVER["HTTP_REFERER"]);
    }

Or

You have to redirect shop-male page with current product id. You can use this code:

$host = $_SERVER['HTTP_HOST'] . '/';
$project = explode('/', $_SERVER['REQUEST_URI'])[1];
header('location: $host.$project.'/shop-male?'.$_GET['id']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM