如何让Apache发送500,以防PHP发生错误? [英] How to let Apache send 500 in case of PHP error?

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

问题描述

我正在开发一个PHP应用程序,它使用HTTP响应代码进行通信以及响应体。所以这是PHP代码中的典型情况:

  try {
doSomething();
}
catch(异常$ e){
header('HTTP / 1.1 500 Internal Server Error');
}

...客户端的典型代码如下所示:

  switch(responseCode){
case 200:
//所有罚款
// ....
案例500:
//麻烦!
}

这是很酷的,只要每个错误都很好地捕获在PHP代码中。



问题:如果由于任何原因在PHP代码中出现未知的错误或无法识别的错误,如语法错误,Apache将发送200好。但是我不知道apache会说500内部服务器错误。也许每个.htaccess等等。

解决方案

当然可以编写自己的错误处理程序。但是,并不是所有的PHP错误都可以捕获。例如,语法错误甚至不能允许您的代码运行,包括错误处理程序。



为了处理可捕获的错误,您可以使用 auto_append_file auto_prepend_file 指令将您的错误处理代码代码。



不可捕获的错误是一个不同的问题。如果PHP作为快速CGI 运行,它将自动生成500个状态代码您。但是,您可能通过其他SAPI(如Apache模块)运行PHP。不知道,对不起。 (如果我找到东西,我会回报。)


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.

解决方案

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.

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

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.)

这篇关于如何让Apache发送500,以防PHP发生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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