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

后端 未结 10 1509
孤独总比滥情好
孤独总比滥情好 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 10:53

    You would have to catch the thrown error using try/catch and then use that catch block to send a header() with the 500 error.

    try {
        ...badcode...
        throw new Exception('error');
    
    } catch (Exception $e) {
    
        header("Status: 500 Server Error");
        var_dump($e->getMessage());
    }
    

    If the fatal exception is not surrounded by try {} catch blocks then you must register a global handler and use register_shutdown_function() to check for an error at script end.

提交回复
热议问题