使用 .htaccess 处理 PHP 错误

发布于 2025-01-05 00:06:27 字数 357 浏览 2 评论 0原文

我读过几篇有关网站安全的文章,他们建议将此代码添加到您的 .htaccess 文件中,以防止显示 PHP 错误:

# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

如果我直接 进入我的 .htaccess 文件时,出现 500 内部服务器错误。有什么问题吗?这些都是已弃用的东西吗?

I've been reading a couple of articles on website security and they recommend adding this code to your .htaccess file to prevent the display of PHP errors:

# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0

If I add this code directly into my .htaccess file I am given a 500 internal server error. What's wrong with it? Is this all deprecated stuff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯沦落人 2025-01-12 00:06:27

事实上,它确实依赖于 PHP SAPI。这种精确的 .htaccess 语法仅适用于 mod_php 设置,不适用于 CGIFastCGI 安装。在后一种情况下,您将使用 .user.ini(对于 PHP 5.3 及以上版本)。

不过,您拥有的大多数选项都可以在运行时配置。在调用脚本顶部使用 ini_set()

ini_set("display_errors", 0);

请注意,对于 _startup_errors 那里配置显然已经太晚了。如果 display_errors 已经关闭,那么禁用 html_errors 和 docref 也是多余的。

It does in fact depend on the PHP SAPI. This precise .htaccess syntax will only work with mod_php setups, not with CGI or FastCGI installations. In the latter case you would use a .user.ini (for PHP 5.3 onwards) instead.

Most of the options you have there can however be configured at runtime. Use ini_set() atop the invocation script:

ini_set("display_errors", 0);

Note that for _startup_errors it's obviously too late to be configured there. Also it's redundant to disable html_errors and the docref things if display_errors is already off.

简美 2025-01-12 00:06:27

为了安全起见,最好不要显示错误,但请记住确保启用了日志记录,以便您可以看到站点中发生的错误并追踪其来源。

在 .htaccess 中,您可以编写:

php_flag display_errors off
php_flag log_errors on
php_flag track_errors on
php_value error_log /path/php_error_log

另外,由于您使用 .htaccess,您可以拥有自己的自定义错误页面:

ErrorDocument 401 /error401.html
ErrorDocument 403 /error403.html
ErrorDocument 404 /error403.html
ErrorDocument 500 /error500.html

For security, it's better not to display the errors but remember to make sure their logging is enabled, so you can see the errors happening in the site and trace their sources.

In .htaccess you can write:

php_flag display_errors off
php_flag log_errors on
php_flag track_errors on
php_value error_log /path/php_error_log

Also since you are using .htaccess, you can have your own custom error pages:

ErrorDocument 401 /error401.html
ErrorDocument 403 /error403.html
ErrorDocument 404 /error403.html
ErrorDocument 500 /error500.html
北城孤痞 2025-01-12 00:06:27

最好的选择是禁用 php.ini 中的错误,因为在某些情况下,如果您通过 .htaccess 执行此操作,则会出现内部服务器错误。 ;)

The best option is to disable the errors from php.ini, because in some cases if you do it vía .htaccess will give an Internal Server Error. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文