You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP如何将整数类型变量通过URL传递,实现支付成功后跳转对应详情页

解决方案

核心问题梳理

  • 商品ID$customer['id']的传递链路中断,没有从首页传递到支付成功页
  • PHP单引号包裹的字符串不会解析内部变量,你写的'Refresh: 2; URL=details.php?id=$customer['id']'里的$customer['id']不会被替换为实际数值
  • header()函数必须在任何实际输出(包括HTML标签、空格)之前调用,你现在把它放在HTML的body内部,会触发PHP headers already sent错误,无法生效
  • 原成功页的参数判断存在语法错误,!empty($_GET['tid'] &&的括号位置不对,会导致逻辑判断异常

分步修改代码

1. 调整首页代码,把商品ID传递到支付页

把原首页的跳转链接修改为带ID参数的形式:

<?php 
  $newID = $customer['id']; 
?>
<div class="card-action right-align"> 
  <a class="brand-text" href="payment.php?id=<?php echo $newID; ?>">more info</a>
</div>

2. 调整支付页逻辑,中转商品ID到成功页

在支付页接收上一步传过来的id参数,支付完成后跳转到成功页时,额外拼接cid参数,跳转格式参考:

success.php?tid=xxx&product=xxx&cid=<?php echo (int)$_GET['id']; ?>

对接收的ID做强制类型转换为整数,避免注入风险。

3. 调整支付成功页代码,完成最终跳转

修改后的完整支付成功页代码如下:

<?php
  include 'index.php';
                                    
  if(!empty($_GET['tid']) && !empty($_GET['product']) && !empty($_GET['cid'])) {
    $GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
    $tid = $GET['tid'];
    $product = $GET['product'];
    $cid = (int)$GET['cid']; // 接收商品ID,强制转整数
    // 把header放在所有输出之前,用双引号解析变量
    header("Refresh: 2; URL=details.php?id=$cid");
  } else {
    header('Location: payment.php');
    exit; // 跳转后终止后续代码执行
  }
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <title>Thank You</title>
</head>
<body>
  <div class="container mt-4">
    <h2>Thank you for purchasing <?php echo $product; ?></h2>
    <hr>
    <p>Your transaction ID is <?php echo $tid; ?></p>
    <p>Check your email for more info</p>
  </div>
</body>
</html>

内容的提问来源于stack exchange,提问作者Raza Nayaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 02:18:03