高级管理文档

💡 云策文档标注

概述

本文档介绍了 PHP 中的 display_errors 指令,解释了其作用、在生产环境中禁用的必要性,并提供了在不同服务器配置下禁用该指令的方法。

关键要点

  • display_errors 是 PHP 中的一个指令,用于控制是否在页面上直接打印错误信息。
  • 在生产环境中必须禁用 display_errors,以避免潜在的安全风险,如信息泄露。
  • 禁用方法取决于服务器配置:对于 Apache 模块,可使用 .htaccess 文件;对于 FastCGI/PHP-FPM,可使用 .user.ini 文件。

代码示例

<IfModule mod_php8.c>
    php_flag display_errors off
</IfModule>
display_errors = 0

注意事项

  • 某些主机提供商可能默认启用 display_errors,需检查并手动禁用。
  • 如果禁用方法无效,建议联系主机提供商获取进一步支持。

📄 原文内容

What is display_errors?

display_errors is a directive found in PHP, found in the php.ini file. With this option, PHP determines whether or not errors should be printed directly on the page.

Why does display_errors need to be disabled?

According to PHP documentation, it should never be enabled on production environments or live sites.

While display_errors may provide useful information in debugging scenarios, there are potential security issues that need to be taken into account if it is activated. See OWASP article about improper error handling.

However, some hosting companies have display_errors enabled by default. This may be due to a misconfiguration, such as trying to disable it by using a configuration that does not work in hosting environments where for example PHP is not running as a module, but with PHP FastCGI Process Manager (PHP-FPM).

How to disable display_errors

Check your hosting control panel to disable display_errors or reach out to your hosting provider.

If your PHP is running as Apache module, you may be able to disable display_errors with the following .htaccess configuration:

<IfModule mod_php8.c> php_flag display_errors off </IfModule>

If your server uses FastCGI/PHP-FPM, it may be possible disable the display_errors by ensuring that a .user.ini file with the following content:

display_errors = 0

If these examples do not work for you, or if you need more instructions, please reach out to your hosting provider.