PHP: How to send HTTP response code?

后端 未结 9 1860
无人共我
无人共我 2020-11-22 06:05

I have a PHP script that needs to make responses with HTTP response codes (status-codes), like HTTP 200 OK, or some 4XX or 5XX code.

How can I do this in PHP?

9条回答
  •  攒了一身酷
    2020-11-22 06:07

    If you are here because of Wordpress giving 404's when loading the environment, this should fix the problem:

    define('WP_USE_THEMES', false);
    require('../wp-blog-header.php');
    status_header( 200 );
    //$wp_query->is_404=false; // if necessary
    

    The problem is due to it sending a Status: 404 Not Found header. You have to override that. This will also work:

    define('WP_USE_THEMES', false);
    require('../wp-blog-header.php');
    header("HTTP/1.1 200 OK");
    header("Status: 200 All rosy");
    

提交回复
热议问题