php - 从 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>
我想从用户在处理后在同一页面上单击添加到购物车重定向的地方做
解决方案
$_SERVER['HTTP_REFERER']
在您的标题功能中使用。在此处阅读更多信息:http: //php.net/manual/en/reserved.variables.server.php
推荐阅读
- npm - 在 Nexus NPM 本地 repo 上重新托管 vsix 包
- javascript - 在循环中将处理程序从类传递到组件
- python - 使用 bash 并行运行多个 Python 脚本
- r - dplyr 与 match() 交叉错误:`mutate()` 输入问题 `..1` 1` 必须是向量,而不是函数
- mariadb - Linux 上的 MariaDB 安装程序将自定义 db 文件所有权更改为默认 mysql 并导致数据库关闭
- angular - Angular 12 不会在浏览器中显示错误详细信息,只会显示“编译失败”。
- ruby - 红宝石排列
- php - Laravel 在创建时填充非空字段
- node.js - 如何使用数组中的 id 在 mongoDB 中获取多个文档?
- bash - 将文本列添加到 cat & awk 的结果