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

PHP报错求助:Undefined constant 'success' 问题修复方案

修复PHP中"undefined constant success"的Notice错误

嘿,这个困扰你30分钟的错误其实根源很简单,咱们来快速解决它:

错误原因

你看到的Notice: Use of undefined constant success - assumed 'success'提示,是因为在代码里把字符串success当成了未定义的常量来使用。PHP在遇到这种情况时,会临时把它当作字符串处理,但同时会抛出这个警告。

具体出错的行是这里:

if($proxyid == success) {

这里的success是ip-api返回的字符串状态值,必须用单引号或双引号包裹,否则PHP会认为你在引用一个名为success的常量,但这个常量根本不存在。

修复后的完整代码

同时我还帮你优化了两个小问题:

  1. 修复strtolower($username)没有赋值的问题(原代码调用后没把结果存回变量)
  2. 简化了重复的空值判断逻辑
<?php 
if(isset($_POST['statusBtn'])) { 
    $username = htmlspecialchars($_POST['address']);
    // 修复:把转换后的结果赋值回变量
    $username = strtolower($username); 

    if(empty($username)) { 
        echo '<center><div class="alert alert-icon alert-danger alert-dismissible fade in col-md-7" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>Fill in all fields</div></center>'; 
    } 
    // 优化:去掉重复的空值判断
    else if($username == ""){ 
        echo '<center><div class="alert alert-icon alert-danger alert-dismissible fade in col-md-7" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>Domain Whitelisted</div></center>'; 
    } 
    else { 
        $geo = @file_get_contents("http://ip-api.com/json/{$username}"); 
        $json_a = json_decode($geo, true); 
        $proxyid = $json_a['status']; 
        // 修复:给success加上单引号,作为字符串比较
        if($proxyid == 'success') { 
            echo '<center><div class="alert alert-icon alert-success alert-dismissible fade in col-md-7" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>'.htmlspecialchars($username).' is Online!</div></center>'; 
        } 
        else { 
            echo '<center><div class="alert alert-icon alert-danger alert-dismissible fade in col-md-7" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><i class="mdi mdi-check-all"></i>'.htmlspecialchars($username).' is Offline!</div></center>'; 
        } 
    } 
} 
?>

额外提示

以后在PHP中比较字符串时,一定要记得用引号包裹字符串值,避免把它们当成常量处理。另外,@file_get_contents虽然抑制了错误,但建议在生产环境中加上错误处理逻辑,比如判断$geo是否获取成功,避免后续json_decode出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:14:15