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

PHP主机状态检测代码POST提交失败,请求故障排查

问题排查与解决步骤

一、文件路径与命名检查

  • 确认HTML页面(需改为.php后缀,比如index.php)和test.php处于XAMPP的htdocs目录同一层级,避免因路径不匹配导致无法找到test.php。
  • 检查文件名大小写:Windows系统不区分,但Linux/macOS环境下严格区分,确保表单action中的文件名与实际文件名完全一致(比如test.php不能写成Test.php)。

二、表单提交逻辑优化(解决跳转与路径问题)

当前代码存在结构混乱问题:页面加载时就会include('test.php'),且表单提交后会跳转到test.php单独页面。建议将逻辑整合到同一PHP文件中,避免路径错误:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Computer Status Checker</title>
    <link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="container">
        <h1>Computer Status Checker</h1>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <label for="ipAddress">Enter IP Address:</label>
            <input type="text" id="ipAddress" name="ipAddress" required>
            <button type="submit">Check Status</button>
        </form>
        <div class="result">
            <?php
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (!empty($_POST["ipAddress"])) {
                    // 安全过滤:防止命令注入
                    $ipAddress = escapeshellarg($_POST["ipAddress"]);
                    $output = shell_exec("ping -c 1 $ipAddress");
                    if (strpos($output, "1 received") !== false) {
                        echo "<p style='color: green;'>Computer is ON</p>";
                    } else {
                        echo "<p style='color: red;'>Computer is OFF</p>";
                    }
                } else {
                    echo "<p style='color: red;'>Please enter an IP address</p>";
                }
            }
            ?>
        </div>
    </div>
</body>
</html>

修改后无需单独的test.php,提交表单后直接在原页面显示结果,同时解决路径问题。

三、Apache环境配置检查

  • 确认XAMPP控制面板中Apache的PHP模块已启用(在Apache设置中查看mod_php加载状态)。
  • 检查htdocs目录及文件权限:Linux/macOS环境下执行sudo chmod -R 755 /path/to/xampp/htdocs,确保Apache进程有读写权限;Windows环境下一般无需额外设置。
  • 查看Apache错误日志(XAMPP控制面板中Apache的Logs按钮打开error.log),日志会记录具体错误原因(如文件不存在、权限不足等),便于精准定位问题。

四、安全提示

原代码直接将用户输入的IP传入shell_exec,存在命令注入风险,必须用escapeshellarg()对用户输入进行过滤,避免恶意命令执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 01:05:03