有没有办法重定向到register_shutdown_function中的错误页面? [英] Is there a way to redirect to an error page in register_shutdown_function?

查看:105
本文介绍了有没有办法重定向到register_shutdown_function中的错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个干净的方法来处理致命和严重的错误(即没有输出缓冲),我读到在register_shutdown_function中使用HTTP头是不可能的。



事情是,当发生严重错误时,我想重定向到一个通用的静态错误页面(例如:服务不可用)。



我该怎么办?



显示或使用Javascript似乎不令人满意的解决方案。

感谢您的帮助!

解决方案

有一个解决方案(这确实会检测到解析错误),与您所读取的内容相反:



我在生产服务器上运行一个网站,并且至关重要的是我们不显示如果确实有致命错误,任何错误也不会显示任何白皮书。目前,我们没有测试服务器,所以很难弄清楚。



生产就绪PHP错误处理



我们网站上显示的每个PHP文件都包含一个位于public_html /目录上方一步的文件base.php,所以想法是在base.php的顶部设置一个错误处理程序其他任何东西:



base.php - 将其插入到代码的顶部:

 
//不要向用户显示错误
ini_set(display_errors,0);
ini_set(log_errors,1);

//定义您要在哪里登录,syslog或您喜欢的文件,
ini_set(error_log,dirname(__ FILE __)。'/ php_errors.log') ;

register_shutdown_function(function(){
$ last_error = error_get_last();
if(!empty($ last_error)&&
$ last_error ['type']& (E_ERROR | E_COMPILE_ERROR | E_PARSE | E_CORE_ERROR | E_USER_ERROR)

{
require_once(dirname(__ FILE __)。'/ public_html / maintenance.php');
exit(1) ;
}
});

现在,base.php的想法根本不包括很多代码,没有功能,只有其他包含或必需的网站文件跑步。
在测试我的例子中,我有一些类似

 include_once(dirname(__ FILE __)。'/ public_html / lib / foobar.php'); 

所以我去了foobar.php并发出语法错误,将功能 ...转换为 fun ction 即可。
通常会显示一个错误或者显示一个白页,但是由于register_shutdown_function(),现在public_html / maintenance.php页面显示了一条消息,例如:



对不起,我们目前遇到技术上的困难。请在几分钟后刷新此页面,看看我们是否解决了这个问题。



当然,用户不知道这是维护。 php



干杯!


I'm trying to find a clean way to handle fatal and critical errors (i.e. without output buffering), and I read that using HTTP headers in register_shutdown_function is impossible.

The thing is, I'd like to redirect to a generic static error page when a critical error occurred (e.g.: service unavailable).

How should I do ?

Display a or using Javascript do not seem satisfactory solutions.

Thanks for your help !

解决方案

There is a Solution to this (that does detect parse errors), contrary to what you have read:

I run a website on a production server and it is critical that we not display any errors nor show any whitepages if indeed there is a fatal error. At the current time, we do not have a testing server, so it was a pain to figure out.

Production-Ready PHP Error Handling

Each PHP file that gets displayed on our website includes a file base.php located one step above the public_html/ directory, so the idea was to set an error handler at the very top of base.php before anything else:

base.php -- insert this at the top of your code:

//DO NOT DISPLAY ERRORS TO USER
ini_set("display_errors", 0);
ini_set("log_errors", 1);

//Define where do you want the log to go, syslog or a file of your liking with
ini_set("error_log", dirname(__FILE__).'/php_errors.log');

register_shutdown_function(function(){
    $last_error = error_get_last();
    if ( !empty($last_error) && 
         $last_error['type'] & (E_ERROR | E_COMPILE_ERROR | E_PARSE | E_CORE_ERROR | E_USER_ERROR)
       )
    {
       require_once(dirname(__FILE__).'/public_html/maintenance.php');
       exit(1);
    }
});

Now the idea of base.php is not to include much code at all, no functions, only other included or required files for the site to run. In testing my example, i had something like

include_once(dirname(__FILE__).'/public_html/lib/foobar.php');

So i went to foobar.php and made a syntax error, turning function... into fun ction. Normally an error would display or a whitepage would display, but because of register_shutdown_function(), now the public_html/maintenance.php page shows up with a message with something like:

Sorry, we are currently experiencing technical difficulties. Please refresh this page in a few minutes to see if we have resolved the issue.

And of course the user has no idea that this is "maintenance.php"

Cheers!

这篇关于有没有办法重定向到register_shutdown_function中的错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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