How can I get php to return 500 upon encountering a fatal exception?

后端 未结 10 1508
孤独总比滥情好
孤独总比滥情好 2020-12-24 10:37

PHP fatal errors come back as status code 200 to the HTTP client. How can I make it return a status code 500 (Internal server error)?

10条回答
  •  盖世英雄少女心
    2020-12-24 11:10

    I have used "set_exception_handler" to handle uncaught exceptions.

    function handleException($ex) {
          error_log("Uncaught exception class=" . get_class($ex) . " message=" . $ex->getMessage() . " line=" . $ex->getLine());
          ob_end_clean(); # try to purge content sent so far
          header('HTTP/1.1 500 Internal Server Error');
          echo 'Internal error';
        }
    
    set_exception_handler('handleException');
    

提交回复
热议问题