使用Lighttpd和Fast-CGI时如何显示PHP错误? [英] How to display PHP errors when using Lighttpd and Fast-CGI?

查看:93
本文介绍了使用Lighttpd和Fast-CGI时如何显示PHP错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FastCGI运行Lighttpd网络服务器,并且该网络服务器不输出PHP解析错误.

I'm running a Lighttpd webserver using FastCGI and the webserver does not output PHP Parse Errors.

我的php.ini文件具有以下设置:

My php.ini file has the following settings:

error_reporting = E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = On
html_errors = On

我在这样的PHP脚本中启用错误输出以进行开发(对于其他环境具有冗余性):

I enable error output for development in my PHP scripts like this ( with redundancy for other environments ):

error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
ini_set( 'html_errors', 'On' );

大多数错误输出正常.解析错误没有.下面是引发解析错误的示例代码.该错误不是由Lighttpd Web服务器输出的,而是从命令行执行的,因为它没有使用FastCGI.(请注意缺少的串联运算符):

Most errors output fine. Parse Errors do not. Below is example code that throws a parse error. The error is not outputted by the Lighttpd webserver but is when executed from the command line because it's not using FastCGI. ( notice the missing concatenation operator ):

<?php echo 'foo' 'bar'; ?>

我发现,如果我在php.ini中设置了 display_errors = On ,则可以使用FastCGI和Lighttpd正确解析错误输出,但是我无法使用在我的PHP脚本中将其关闭ini_set('display_errors',0).

I've discovered that if I set display_errors = On in php.ini then parse errors output correctly with FastCGI and Lighttpd, but then I cannot turn them off within my PHP scripts using ini_set( 'display_errors', 0 ).

我希望能够在开发应用程序时在应用程序中显示这些内容,并能够在不更改php.ini配置的情况下将其关闭以进行生产.使用FastCGI时,无法在我的PHP应用程序中执行此操作吗?

I'd like to be able to display these within my application when developing it, and be able to turn them off for production without changing php.ini configurations. Is there no way to do this within my PHP application when using FastCGI?

推荐答案

之所以没有显示错误,是因为当代码(例如您的示例)中出现致命错误时,根本不执行该代码,因此从不设置display_errors值.

The reason errors aren't being displayed is because when a fatal error is present in the code (such as your example), the code is not executed at all, and hence the display_errors value is never set.

从php.net( http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors ):

From php.net (http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors):

尽管display_errors可以在运行时设置(使用ini_set()),但是如果脚本有致命错误,它将不会有任何影响.这是因为所需的运行时操作未执行.

Although display_errors may be set at runtime (with ini_set()), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.

我发现的一个解决方案是在您正在开发的目录中使用.user.ini文件,并希望看到错误输出(它们的功能类似于Apache的.htaccess文件).没有一种修改PHP代码以显示致命错误的好方法,因为永远不会运行包含致命错误的代码.

A solution that I have found is to use .user.ini files in directories where you are developing and would like to see error output (they function like .htaccess files for Apache). There isn't a good way to modify PHP code to display fatal errors, as code that contains a fatal error is never run.

因此,在上面的示例中,您可以在与包含以下行的php文件相同的目录中使用.user.ini文件:

So for your example above you could use a .user.ini file in the same directory as your php file containing the lines:

error_reporting = -1
display_errors = On
html_errors = On

有关更多信息,请参见 http://php.net/manual/zh/configuration.file.per-user.php

For more info see http://php.net/manual/en/configuration.file.per-user.php

这篇关于使用Lighttpd和Fast-CGI时如何显示PHP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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