【问题标题】:Enabling error display in PHP via htaccess only仅通过 htaccess 在 PHP 中启用错误显示
【发布时间】:2011-09-01 22:45:06
【问题描述】:

我正在在线测试一个网站。

现在,错误没有显示(但我知道它们存在)。

我只能访问.htaccess 文件。

如何使用我的.htaccess 文件显示所有错误?


我将这些行添加到我的.htaccess 文件中:

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

页面现在显示:

内部服务器错误

【问题讨论】:

  • 我做了一些谷歌搜索,在我的 htaccess 中添加了一些标志;我的页面无法再次显示
  • 似乎您无权从 htaccess 覆盖这些设置。如果您有权访问,您可能需要在 Apache 配置中设置 AllowOverride All
  • 检查您的 apache 错误日志。必须确切说明您收到 500 内部错误的原因。您在浏览器中看到的内容设计用于诊断目的。

标签: php .htaccess error-handling


【解决方案1】:

这对我有用 (reference):

# PHP error handling for production servers
# Disable display of startup errors
php_flag display_startup_errors off

# Disable display of all other errors
php_flag display_errors off

# Disable HTML markup of errors
php_flag html_errors off

# Enable logging of errors
php_flag log_errors on

# Disable ignoring of repeat errors
php_flag ignore_repeated_errors off

# Disable ignoring of unique source errors
php_flag ignore_repeated_source off

# Enable logging of PHP memory leaks
php_flag report_memleaks on

# Preserve most recent error via php_errormsg
php_flag track_errors on

# Disable formatting of error reference links
php_value docref_root 0

# Disable formatting of error reference links
php_value docref_ext 0

# Specify path to PHP error log
php_value error_log /home/path/public_html/domain/PHP_errors.log

# Specify recording of all PHP errors
# [see footnote 3] # php_value error_reporting 999999999
php_value error_reporting -1

# Disable max error string length
php_value log_errors_max_len 0

# Protect error log by preventing public access
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>

【讨论】:

    【解决方案2】:

    我想在现有答案中添加更多细节:

    # PHP error handling for development servers
    php_flag display_startup_errors on
    php_flag display_errors on
    php_flag html_errors on
    php_flag log_errors on
    php_flag ignore_repeated_errors off
    php_flag ignore_repeated_source off
    php_flag report_memleaks on
    php_flag track_errors on
    php_value docref_root 0
    php_value docref_ext 0
    php_value error_log /full/path/to/file/php_errors.log
    php_value error_reporting -1
    php_value log_errors_max_len 0
    

    给日志文件777或755权限,然后添加代码

    <Files php_errors.log>
         Order allow,deny
         Deny from all
         Satisfy All
    </Files>
    

    在 .htaccess 的末尾。 这将保护您的日志文件。

    这些选项适用于开发服务器。对于生产服务器,您不应向最终用户显示任何错误。因此,将显示标志更改为 off

    欲了解更多信息,请点击此链接:Advanced PHP Error Handling via htaccess

    【讨论】:

    • 给错误日志文件 chmod "0644" 怎么样?它还能工作吗?
    【解决方案3】:

    如果您只想查看致命的运行时错误:

    php_value display_errors on
    php_value error_reporting 4
    

    【讨论】:

      【解决方案4】:
      php_flag display_errors on
      

      打开错误的实际显示。

      要设置您显示的错误类型,您需要使用:

      php_value error_reporting <integer>
      

      结合本页的整数值:http://php.net/manual/en/errorfunc.constants.php

      请注意,如果您使用 -1 作为整数,它将显示所有错误,并在它们添加新类型的错误时成为未来的证明。

      【讨论】:

      【解决方案5】:

      .htaccess:

      php_flag display_startup_errors on
      php_flag display_errors on
      php_flag html_errors on
      php_flag  log_errors on
      php_value error_log  /home/path/public_html/domain/PHP_errors.log
      

      【讨论】:

      • 我在我的 htaccess 中添加了这些行: php_flag display_startup_errors on php_flag display_errors on php_flag html_errors on 并且页面显示内部服务器错误
      • 当使用 PHP 作为 Apache 模块时,您还可以使用 Apache 配置文件(例如 httpd.conf)和 .htaccess 文件中的指令更改配置设置。您将需要“AllowOverride Options”或“AllowOverride All”权限才能执行此操作。 php.net/manual/en/configuration.changes.php
      • 创建 PHP_errors.log 并将其设置为 777 其他方式您可能不会看到由 apache 创建并填充的文件...至少我必须创建它。
      • 像 ogugua 我现在也有内部服务器错误使用这些行。
      • 如果你得到 500,那可能是因为你使用的是 php-fpm,而不是 mod_php。
      猜你喜欢
      • 2013-10-30
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2014-05-13
      • 1970-01-01
      相关资源
      最近更新 更多