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

网站出现Undefined index与Cannot modify header错误的修复求助

问题分析

你遇到的两类错误根源如下:

  1. Undefined index警告:当支付失败或用户关闭结账模态框时,Flutterwave的API响应可能未返回data字段,或者data下缺少status、chargecode等关键字段,直接访问这些不存在的索引就会触发警告。
  2. Header跳转失效:Undefined index的警告信息会提前输出到页面,导致HTTP响应头已被发送,后续header()函数无法执行,跳转逻辑失效。

修复方案

1. 安全提取API响应字段

在读取$resp['data']下的字段前,先判断字段是否存在,避免Undefined index错误:

// 替换原有的字段提取代码
$paymentStatus = isset($resp['data']['status']) ? $resp['data']['status'] : null;
$chargeResponseCode = isset($resp['data']['chargecode']) ? $resp['data']['chargecode'] : null;
$chargeAmount = isset($resp['data']['amount']) ? $resp['data']['amount'] : null;
$chargeCurrency = isset($resp['data']['currency']) ? $resp['data']['currency'] : null;

如果你的PHP版本≥7.0,也可以用更简洁的空合并运算符:

$paymentStatus = $resp['data']['status'] ?? null;
$chargeResponseCode = $resp['data']['chargecode'] ?? null;
$chargeAmount = $resp['data']['amount'] ?? null;
$chargeCurrency = $resp['data']['currency'] ?? null;

2. 关闭Notice级错误输出

在脚本开头添加代码,抑制Notice警告,避免提前输出内容导致Header失效:

// 放在include语句前
error_reporting(E_ALL & ~E_NOTICE);

3. 完善响应有效性判断

提前检查API响应是否完整,若响应无效直接跳转失败页面:

// 在字段提取后添加
if (!isset($resp['data']) || is_null($chargeResponseCode) || is_null($chargeAmount) || is_null($chargeCurrency)) {
    header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
    exit;
}

4. 检查CURL请求是否成功

还要考虑网络问题导致CURL请求失败的情况,此时$response为false,json_decode后$resp为null,需提前拦截:

$response = curl_exec($ch);

// 添加CURL错误检查
if ($response === false) {
    curl_close($ch);
    header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
    exit;
}

curl_close($ch);

$resp = json_decode($response, true);
// 检查JSON解码是否成功
if ($resp === null) {
    header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
    exit;
}

完整修复代码

整合所有修改后的完整脚本:

error_reporting(E_ALL & ~E_NOTICE);
include "registerdbctrl.php";
if (isset($_GET['txref'])) {
    $ref = $_GET['txref'];
    $amount = $_GET['amount'];
    $email = $_GET['email'];
    $currency = "NGN";
    $registerid = $_GET['registerid'];

    $query = array(
        "SECKEY" => "FLWSECK_TEST-0498dfe49bdbd603460424be3a7b94e6-X",
        "txref" => $ref
    );

    $data_string = json_encode($query);

    $ch = curl_init('https://api.ravepay.co/flwv3-pug/getpaidx/api/v2/verify');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                              
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    $response = curl_exec($ch);

    if ($response === false) {
        curl_close($ch);
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
        exit;
    }

    curl_close($ch);

    $resp = json_decode($response, true);
    if ($resp === null) {
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
        exit;
    }

    $paymentStatus = $resp['data']['status'] ?? null;
    $chargeResponseCode = $resp['data']['chargecode'] ?? null;
    $chargeAmount = $resp['data']['amount'] ?? null;
    $chargeCurrency = $resp['data']['currency'] ?? null;

    if (!isset($resp['data']) || is_null($chargeResponseCode) || is_null($chargeAmount) || is_null($chargeCurrency)) {
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
        exit;
    }

    if (($chargeResponseCode == "00" || $chargeResponseCode == "0") && ($chargeAmount == $amount)  && ($chargeCurrency == $currency)) {
        $payStatus = "payment successful";
        $sql = "UPDATE registertbl SET paymentStatus = ? WHERE registerid = ?";
        $stmt = $con->prepare($sql);
        $stmt->bind_param("ss", $pstatus, $pregisterid);
        $pstatus = $payStatus;
        $pregisterid = $registerid;
        $stmt->execute();
        
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/success.php');
        exit;
    } else {
        header('location:https://bnbtechdigitalskillstraining.000webhostapp.com/error.php');
        exit;
    }
} else {
    die('No reference supplied');
}
?>

内容的提问来源于stack exchange,提问作者Ndongesit Edet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 18:44:51