Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用.htaccess处理PHP错误_Php_Apache_Security_.htaccess - Fatal编程技术网

使用.htaccess处理PHP错误

使用.htaccess处理PHP错误,php,apache,security,.htaccess,Php,Apache,Security,.htaccess,我已经阅读了几篇关于网站安全性的文章,他们建议将此代码添加到.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内

我已经阅读了几篇关于网站安全性的文章,他们建议将此代码添加到
.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内部服务器错误。怎么了?这都是不推荐使用的东西吗?

最好的选择是禁用php.ini中的错误,因为在某些情况下,如果您这样做,vía.htaccess将给出一个内部服务器错误

这实际上取决于环境。这种精确的
.htaccess
语法仅适用于
mod_php
设置,而不适用于
CGI
FastCGI
安装。在后一种情况下,您将使用a(对于PHP5.3以后的版本)

但是,可以在运行时配置大多数选项。使用调用脚本:

ini_set("display_errors", 0);

请注意,对于
\u启动\u错误
来说,在那里配置显然太晚了。另外,禁用
html\u错误
是多余的,如果
display\u errors
已关闭,则docref内容也会被禁用。

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

在.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

将该代码,
ini\u设置(“显示错误”,0)禁用所有php错误?它将禁用所有错误的显示。使用默认的错误处理程序。它不影响日志记录或自定义错误处理程序。和
错误报告(0)将有相同的结果。关闭html错误不是多余的。PHP也乐于将HTML写入文本日志:)禁用错误报告与掩盖事实一样,也是一种安全风险。记录错误可以识别问题并解决它们。