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

PHP设置http_response_code(200)无效,httpstatus.io仍返回404求解决方案

Fixing PHP's http_response_code() Not Overriding 404 Status

Hey, I’ve run into this exact issue before—let’s break down why your http_response_code(200) isn’t sticking and how to fix it:

  • Ensure you call it before any output
    http_response_code() only works if it’s executed before any content is sent to the browser—this includes HTML, whitespace, even accidental spaces before your opening <?php tag. If headers have already been sent, PHP can’t modify them. Try moving the function to the absolute top of your file:

    <?php
    http_response_code(200);
    // Rest of your code below
    ?>
    

    Also check if your framework, CMS, or any included scripts are sending a 404 header earlier in the execution flow.

  • Check server/.htaccess rules
    Server configurations (like Apache’s .htaccess) might be forcing a 404 before PHP even runs. For example, if you have an ErrorDocument 404 /custom-404.php rule, and your page is that custom-404.php, the server sends a 404 header automatically. Try temporarily commenting out such rules to test if that’s the issue.

  • Use the header() function as a fallback
    Some older PHP versions or server environments have compatibility issues with http_response_code(). Switch to the more universal header() function to set the status directly:

    <?php
    // For HTTP/1.1
    header("HTTP/1.1 200 OK");
    // Or for HTTP/2 compatibility
    header("Status: 200 OK");
    ?>
    

    Again, make sure this runs before any output.

  • Verify the file exists and is accessible
    If the server can’t find your PHP file at all, it’ll return a 404 before your code ever executes. Double-check the request URL points to the correct file, and that the file has proper read permissions for the server user.

  • Test locally first
    Don’t rely solely on external tools like httpstatus.io—use your browser’s DevTools (Network tab) or the curl command to inspect the actual response headers:

    curl -I https://your-domain.com/your-target-page.php
    

    This will give you a direct view of what the server is sending, ruling out tool caching or external proxy issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:53:15