I'm developing a PHP application that uses HTTP response codes for communication as well as response bodies. So this is a typical scenario in the PHP code:
try {
doSomething();
}
catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
}
... and the typical code in clients looks like:
switch(responseCode) {
case 200 :
// all fine
// ....
case 500 :
// trouble!
}
This is cool, as long as every error is well caught in PHP code.
The problem: If, for any reason in the php code occures an uncaught error or an uncatchable error like syntax errors, Apache will send 200 OK. But I wan't apache to say 500 Internal Server Error. Maybe per .htaccess or so.
Вы, конечно, можете написать свой собственный обработчик ошибок . Однако не все ошибки PHP можно отловить. Например, синтаксическая ошибка не позволит запустить ваш код, включая обработчик ошибок.
Для обработки обнаруживаемых ошибок вы можете использовать директивы auto_append_file и auto_prepend_file для размещения кода обработки ошибок.
Неуловимые ошибки - это другая проблема. Если PHP работает как Fast CGI , он автоматически сгенерирует для вас код состояния 500. Однако вы, вероятно, используете PHP через какой-то другой SAPI (например, модуль Apache). Не знаю, извините. (Я сообщу, если что-нибудь найду.)