如何让阿帕奇在PHP错误的情况下送500? [英] How to let Apache send 500 in case of PHP error?

查看:131
本文介绍了如何让阿帕奇在PHP错误的情况下送500?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用HTTP响应codeS通信和响应主体PHP应用程序。因此,这是在PHP code的典型场景:

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');
}

...在客户端上的典型code如下:

... and the typical code in clients looks like:

switch(responseCode) {
     case 200 :
         // all fine
         // ....
     case 500 :
         // trouble!
 } 

这是很酷的,只要每一个错误以及夹在PHP code。

This is cool, as long as every error is well caught in PHP code.

问题:如果出于某种原因在php code occures未被捕获的错误或一个不可捕获的错误,如语法错误,Apache将发送200 OK。但我wan't阿帕奇说500内部服务器错误。也许每个的.htaccess左右。

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错误是开捕。例如,语法错误,甚至不会让你的code运行,包括您的错误处理程序。

You can, of course, write your own error handler. However, not all PHP errors are catchable. For instance, a syntax error won't even allow your code to run, including your error handler.

要处理开捕错误,可以使用的 auto_append_file所 AUTO_ prepend_file 指令,把你的错误处理code code。

To handle catchable errors, you can use the auto_append_file and auto_prepend_file directives to place your error handling code code.

非开捕错误是一个不同的问题。如果PHP运行作为快速CGI ,它会自动生成一个500状态code表示你。不过,你可能会通过一些其他的SAPI(如Apache模块)运行PHP。关于不知道,对不起。 (如果我找东西,我会汇报。)

Non-catchable errors are a different issue. If PHP runs as Fast CGI, it will automatically generate a 500 status code for you. However, you're probably running PHP through some other SAPI (such as Apache module). No idea about that, sorry. (I'll report back if I find something.)

这篇关于如何让阿帕奇在PHP错误的情况下送500?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆