如何打开仅在子文件夹上显示的 PHP 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/107828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:28:43  来源:igfitidea点击:

How can I turn on PHP errors display on just a subfolder

php

提问by Kent Fredric

I don't want PHPerrors to display /html, but I want them to display in /html/beta/usercomponent. Everything is set up so that errors do not display at all. How can I get errors to just show up in that one folder (and its subfolders)?

我不希望PHP错误显示 /html,但我希望它们显示在/html/beta/usercomponent. 一切都已设置好,根本不会显示错误。我怎样才能让错误只显示在那个文件夹(及其子文件夹)中?

回答by Kent Fredric

In .htaccess:

.htaccess

php_value error_reporting 2147483647

This number, according to documentation should enable 'all' errors irrespective of version, if you want a more granular setting, manually OR the values together, or run

根据文档,此数字应启用“所有”错误,而不管版本如何,如果您想要更精细的设置,请手动将值或值放在一起,或运行

php -r 'echo E_ALL | E_STRICT ;'

to let php compute the value for you.

让 php 为您计算值。

You need

你需要

AllowOverride All

in apaches master configuration to enable .htaccess files.

在 apache 的主配置中启用 .htaccess 文件。

More Reading on this can be found here:

可以在此处找到有关此内容的更多阅读:



NoticeIf you are using Php-CGI instead of mod_php, this may not work as advertised, and all you will get is an internal server error, and you will be left without much option other than enabling it either site-wide on a per-script basis with

注意如果您使用的是 Php-CGI 而不是 mod_php,这可能不会像宣传的那样工作,您将得到的只是内部服务器错误,除了在每个站点范围内启用它之外,您将别无选择脚本基础

error_reporting( E_ALL | E_STRICT ); 

or similar constructs before the error occurs.

或类似的构造在错误发生之前。

My advice is to disabledisplaying errors to the user, and utilize heavily php's error_log feature.

我的建议是禁止向用户显示错误,并大量使用 php 的 error_log 功能。

display_errors = 0
error_logging = E_ALL | E_STRICT 
error_log = /var/log/php 

If you have problems with this being too noisy, this is not a sign you need to just take error reporting off selectively, this is a sign somebody should fix the code.

如果您对此太吵有问题,这不是您需要选择性地取消错误报告的标志,这是应该有人修复代码的标志。



@Roger

@罗杰

Yes, you can use it in a <Directory>construct in apaches configuration too, however, the .htaccess in this case is equivalent, and makes it more portable especially if you have multiple working checkout copies of the same codebase and you want to distribute this change to all of them.

是的,您也可以在 apaches 配置中的构造中使用它,但是,在这种情况下 .htaccess 是等效的,并且使其更具可移植性,尤其是如果您有相同代码库的多个工作签出副本并且您希望将此更改分发给他们都。<Directory>

If you have multiple virtual hosts, you'll want the construct in the respective virtual hosts definition, otherwise, yes

如果您有多个虚拟主机,则需要在各自的虚拟主机定义中使用该构造,否则,是

  <Directory /path/to/wherever/on/filesystem> 
      <IfModule mod_php5.c>
         php_value error_reporting 214748364
      </IfModule>
  </Directory>

The Additional "ifmodule" commands are just a safety net so the above problem with apache dying if you don't have mod_php won't occur.

附加的“ifmodule”命令只是一个安全网,因此如果您没有 mod_php,则不会发生上述 apache 死亡的问题。

回答by Twan

The easiest way would be to control the error reporting from a .htaccess file. But this is assuming you are using Apache and the scripts in /html/beta/usercomponent are called from that directory and not included from elsewhere.

最简单的方法是从 .htaccess 文件控制错误报告。但这是假设您使用的是 Apache 并且 /html/beta/usercomponent 中的脚本是从该目录调用的,而不是从其他地方包含的。

.htacess

.htacess

php_value error_reporting [int]

You will have to compose the integer value yourself from the list as described in the error_reportingdocumentation, since the constants like E_ERROR aren't defined when Apache interprets the .htaccess.

您必须按照error_reporting文档中的说明从列表中自己组合整数值,因为在 Apache 解释 .htaccess 时未定义像 E_ERROR 这样的常量。

It's a simple bitwise flag, so a value of 12, for example, would be E_WARNING + E_PARSE + E_NOTICE.

这是一个简单的按位标志,例如,值 12 将是 E_WARNING + E_PARSE + E_NOTICE。

回答by farzad

you could do this by using an Environment variable. this way you can have more choices than just turning Error reporting on/off for a special directory. in your code where ever you wanted to change any behaviour for a specific set of directoris, or running modes, check if a environment variable is set or not. like this:

你可以通过使用环境变量来做到这一点。通过这种方式,您可以有更多的选择,而不仅仅是为特殊目录打开/关闭错误报告。在您想要更改特定目录集或运行模式的任何行为的代码中,请检查是否设置了环境变量。像这样:

if ($_ENV['MY_PHP_APP_MODE'] == 'devel') {
  // show errors and debugging info
} elseif ($_ENV['MY_PHP_APP_MODE'] == 'production') {
 // show some cool message to the user so he won't freak out
 // log the errors and send email to the admin
}

and when you are running your application in your development environment, you can set an env variable in your .htaccess file like this:

当您在开发环境中运行应用程序时,您可以在 .htaccess 文件中设置一个 env 变量,如下所示:

 setenv MY_PHP_APP_MODE devel

or when you are in production evn:

或者当你在生产中时:

 setenv MY_PHP_APP_MODE production

the same technique applies to your situation. in directories where you want to do something special (turn on error reporting) set some env variable and in your code, check for that.

同样的技术适用于您的情况。在您想要做一些特殊事情(打开错误报告)的目录中设置一些 env 变量,并在您的代码中检查它。

回答by Internet Friend

I don't believe there's a simple answer to this, but I'd certainly want to be proven wrong.

我不相信对此有一个简单的答案,但我当然希望被证明是错误的。

edit: turns out this can be controlled from .htaccess files. Thanks people! :)

编辑:原来这可以从 .htaccess 文件中控制。谢谢人们!:)

You can use error_reporting() http://docs.php.net/manual/en/function.error-reporting.phpto switch the setting on a script by script basis, though. If you happen to have a single script which is included every time at /html/beta/usercomponent, this will do the trick.

不过,您可以使用 error_reporting() http://docs.php.net/manual/en/function.error-reporting.php在脚本的基础上切换设置。如果您碰巧有一个脚本,每次都包含在 /html/beta/usercomponent 中,这将起作用。