display_errors设置为Off无效,仍然输出报错

本文介绍了如何解决PHP应用中错误信息泄露的问题,包括修改php.ini、httpd.conf配置及php脚本,针对不同服务器环境(如Apache、IIS、nginx)提供了解决方案,并详细解释了如何排查和修复php-fpm配置中的display_errors参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.xianren.org/web/display_errors-off-no.html


由于使用检测页面异常导致本地路径泄漏,查看360的解决方案:
1、如果是PHP应用程序/Apache服务器,可以通过修改php脚本、配置php.ini以及httpd.conf中的配置项来禁止显示错误信息:
修改php.ini中的配置行: display_errors = off
修改httpd.conf/apache2.conf中的配置行: php_flag display_errors off
修改php脚本,增加代码行: ini_set(‘display_errors’, false);
2、如果是IIS 并且是 支持aspx的环境,可以在网站根目录新建web.config文件(存在该文件则直接修改),或者可以参考这里:http://bbs.webscan.360.cn/forum.php?mod=viewthread&tid=4560&extra=page%3D1
由于使用的是nginx,所以在php.ini的设置:

error_reporting = E_ALL & ~E_NOTICE
display_errors = Off
log_errors = On
error_log = /www/logs/php_error.log

按理说这样,错误信息是不会输出了,但是当PHP有错误时,会把报错提示显示在页面上。
搜索了下,发现有人说当log_errors开启时,如果error_log的路径不对,会导致报错显示。经查与此无关。
输出phpinfo(),查看到display_errors是On。在页面中指定:ini_set(“DISPLAY_ERRORS”,0),仍然无效!
最后发现php-fpm.conf里有句:
引用

Additional php.ini defines, specific to this pool of workers.
<value name="php_defines">
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
<value name="display_errors">0</value>
</value>

原来忘了修改这个地方的配置,display_errors被设置为1了!改成0就好了。这里面也说明了在php_defines里可以额外指定某些php的参数。
直接将这两段注释掉,php-fpm reload后,报错提示不再显示到页面上了。


[0]**** Build of configuration Debug for project Lab4 **** [1]"C:\\ti\\ccs2010\\ccs\\utils\\bin\\gmake" -k -j 16 all -O [2]Building file: "../main.c" [3]Invoking: MSP430 Compiler [4]"C:/ti/ccs2010/ccs/tools/compiler/ti-cgt-msp430_21.6.1.LTS/bin/cl430" -vmspx --data_model=restricted --use_hw_mpy=F5 --include_path="C:/ti/ccs2010/ccs/ccs_base/msp430/include" --include_path="C:/Users/14665/workspace_ccstheia/Lab4" --include_path="C:/Users/14665/workspace_ccstheia/Lab4/driverlib/MSP430F5xx_6xx" --include_path="C:/ti/ccs2010/ccs/tools/compiler/ti-cgt-msp430_21.6.1.LTS/include" --advice:power="none" --define=__MSP430F5529__ --define=DEPRECATED -g --printf_support=minimal --diag_warning=225 --diag_wrap=off --display_error_number --silicon_errata=CPU21 --silicon_errata=CPU22 --silicon_errata=CPU23 --silicon_errata=CPU40 --preproc_with_compile --preproc_dependency="main.d_raw" "../main.c" [5]>> Compilation failure [6]subdir_rules.mk:9: recipe for target 'main.obj' failed [7]"../main.c", line 26: error #20: identifier "CSCTL0_H" is undefined [8]"../main.c", line 26: error #20: identifier "CSKEY_H" is undefined [9]"../main.c", line 27: error #20: identifier "CSCTL1" is undefined [10]"../main.c", line 27: error #20: identifier "DCOFSEL_6" is undefined [11]"../main.c", line 28: error #20: identifier "CSCTL2" is undefined [12]"../main.c", line 29: error #20: identifier "CSCTL3" is undefined [13]"../main.c", line 46: error #20: identifier "P4SEL1" is undefined [14]"../main.c", line 47: error #20: identifier "P4SEL0" is undefined [15]"../main.c", line 55: error #20: identifier "UCA1MCTLW" is undefined [16]"../main.c", line 91: warning #1-D: last line of file ends without a newline [17]9 errors detected in the compilation of "../main.c". [18]gmake: *** [main.obj] Error 1 [19]gmake: Target 'all' not remade because of errors. [20]**** Build Finished 报错
06-16
#include <stdio.h> #include <stdlib.h> #pragma pack(push, 1) typedef struct { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned int bfOffBits; } BITMAPFILEHEADER; typedef struct { unsigned int biSize; unsigned int biWidth; unsigned int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; unsigned int biXPelsPerMeter; unsigned int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } BITMAPINFOHEADER; #pragma pack(pop) // C6000兼容的周期计数函数 unsigned long long get_cycle_count() { unsigned int tsch, tscl; asm(" mvc .s2 TSCH, %0" : "=r"(tsch)); asm(" mvc .s2 TSCL, %0" : "=r"(tscl)); return ((unsigned long long)tsch << 32) | tscl; } int main(void) { const int width = 768, height = 576; const int sizeImage = width * height * 3; const int iterations = 1000; const char *src1_path = "HazyDay1.bmp"; const char *src2_path = "HazyDay2.bmp"; const char *dst_path = "jieguo.bmp"; printf("Starting image processing on C6000 DSP...\n"); printf("Image size: %dx%d, Iterations: %d\n", width, height, iterations); FILE *src1 = fopen(src1_path, "rb"); FILE *src2 = fopen(src2_path, "rb"); FILE *dst = fopen(dst_path, "wb"); if (!src1 || !src2 || !dst) { printf("Error opening files\n"); if (src1) fclose(src1); if (src2) fclose(src2); if (dst) fclose(dst); return -1; } BITMAPFILEHEADER fileHeader; BITMAPINFOHEADER infoHeader; if (fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, src1) != 1 || fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, src1) != 1) { printf("Error reading BMP headers\n"); fclose(src1); fclose(src2); fclose(dst); return -1; } unsigned char *img1 = (unsigned char *)malloc(sizeImage); unsigned char *img2 = (unsigned char *)malloc(sizeImage); unsigned char *result = (unsigned char *)malloc(sizeImage); if (!img1 || !img2 || !result) { printf("Memory allocation failed\n"); free(img1); free(img2); free(result); fclose(src1); fclose(src2); fclose(dst); return -1; } fseek(src1, fileHeader.bfOffBits, SEEK_SET); fseek(src2, fileHeader.bfOffBits, SEEK_SET); fread(img1, 1, sizeImage, src1); fread(img2, 1, sizeImage, src2); unsigned long long start_cycles, end_cycles; double total_time_3ch = 0, total_time_1ch = 0; int i, iter; // ================= 三通道同时处理 ================= printf("Running 3-channel method...\n"); start_cycles = get_cycle_count(); for (iter = 0; iter < iterations; iter++) { unsigned char *p1 = img1; unsigned char *p2 = img2; unsigned char *res = result; for (i = 0; i < sizeImage; i++) { *res++ = (*p1++ + *p2++) >> 1; } } end_cycles = get_cycle_count(); total_time_3ch = (double)(end_cycles - start_cycles); // ================= 单通道分别处理 ================= printf("Running 1-channel method...\n"); start_cycles = get_cycle_count(); for (iter = 0; iter < iterations; iter++) { for (i = 0; i < sizeImage; i += 3) { result[i] = (img1[i] + img2[i]) >> 1; result[i+1] = (img1[i+1] + img2[i+1]) >> 1; result[i+2] = (img1[i+2] + img2[i+2]) >> 1; } } end_cycles = get_cycle_count(); total_time_1ch = (double)(end_cycles - start_cycles); // 输出结果 printf("\nProcessing Time Comparison (after %d iterations):\n", iterations); printf("3-Channel Method:\n"); printf(" Total cycles: %.0f\n", total_time_3ch); printf(" Average cycles: %.0f\n", total_time_3ch/iterations); printf("1-Channel Method:\n"); printf(" Total cycles: %.0f\n", total_time_1ch); printf(" Average cycles: %.0f\n", total_time_1ch/iterations); if (total_time_1ch > 0 && total_time_3ch > 0) { double improvement = (total_time_1ch - total_time_3ch) / total_time_1ch * 100; printf("Efficiency Improvement: %.2f%% (%s method is faster)\n", fabs(improvement), improvement > 0 ? "3-channel" : "1-channel"); } // 写入结果 fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, dst); fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, dst); fseek(dst, fileHeader.bfOffBits, SEEK_SET); fwrite(result, 1, sizeImage, dst); // 释放资源 free(img1); free(img2); free(result); fclose(src1); fclose(src2); fclose(dst); return 0; } 为什么有下面的报错 **** Build of configuration Debug for project lab_03_app **** "G:\\ccs\\CCS5.5.0.00077_win32\\ccsv5\\utils\\bin\\gmake" -k all 'Building file: ../main.c' 'Invoking: C6000 Compiler' "G:/ccs/CCS5.5.0.00077_win32/ccsv5/tools/compiler/c6000_7.4.4/bin/cl6x" -mv6740 --abi=coffabi -g --include_path="G:/ccs/CCS5.5.0.00077_win32/ccsv5/tools/compiler/c6000_7.4.4/include" --display_error_number --diag_warning=225 --diag_wrap=off --preproc_with_compile --preproc_dependency="main.pp" "../main.c" "../main.c", line 31: error #18: expected a ")" "../main.c", line 32: error #18: expected a ")" "../main.c", line 33: warning #551-D: variable "tsch" is used before its value is set "../main.c", line 33: warning #551-D: variable "tscl" is used before its value is set 2 errors detected in the compilation of "../main.c". >> Compilation failure gmake: *** [main.obj] Error 1 gmake: Target `all' not remade because of errors. **** Build Finished ****
06-06
<think>好的,我现在需要帮助用户解决如何在PHP中设置display_errors的配置方法,以及确定配置文件的位置。用户之前提到了在php.ini中设置display_errorsoff但未生效的问题,并引用了多个相关讨论。我需要结合这些引用信息,给出准确的配置步骤和常见问题解决方案。 首先,用户的问题明确:他们想知道在哪里设置PHP的display_errors,特别是配置文件的位置。根据引用[1]和[3],主要的配置文件是php.ini,但有时候配置可能被其他文件覆盖,比如在PHP-FPM的配置中。引用[2]提到即使php.ini中设置display_errorsoff,但如果log_errors开启且没有正确配置error_log,错误仍然会显示。此外,引用[4]提到使用set_error_handler可能影响错误报告设置,需要特别注意。 我需要分步骤说明配置位置,包括主配置文件php.ini、其他可能的覆盖位置(如.user.ini或虚拟主机配置),以及运行时通过ini_set()函数修改的方法。同时,必须提到相关的配置项,比如display_errors、error_reporting、log_errors和error_log,以及它们之间的依赖关系。例如,如果log_errors开启但error_log不可写,display_errors可能会被绕过。 另外,用户可能遇到的常见问题,如配置未生效,可能的原因包括配置文件未正确加载、权限问题、缓存未清除,或者代码中使用ini_set()或set_error_handler覆盖了配置。需要根据引用中的信息,给出检查配置加载的方法,比如使用phpinfo(),并指导如何确认当前生效的配置。 最后,总结配置的最佳实践,比如生产环境中应关闭display_errors并开启日志记录,开发环境则相反。还要提醒用户注意多个配置文件的优先级,以及使用版本控制工具管理配置的重要性。</think>### PHP中`display_errors`的配置方法及文件位置 --- #### 一、核心配置文件位置 1. **主配置文件 `php.ini`** - **路径**:根据PHP安装方式不同,路径可能为: - Linux:`/etc/php/{版本}/cli/php.ini`(命令行)或`/etc/php/{版本}/fpm/php.ini`(PHP-FPM) - Windows:`C:\php\php.ini` - **验证位置**:通过命令 `php --ini` 或 `<?php phpinfo(); ?>` 查看 `Loaded Configuration File` 字段 - **关键配置项**: ```ini display_errors = Off ; 关闭错误显示(生产环境推荐) error_reporting = E_ALL ; 定义错误报告级别 log_errors = On ; 启用错误日志记录 error_log = /path/to/error.log ; 指定日志文件路径 ``` 2. **其他覆盖配置** - **`.user.ini`文件**(仅Nginx环境): 在网站根目录创建,优先级高于`php.ini`,需设置: ```ini display_errors = Off ``` - **虚拟主机配置**(如Apache): ```apache <VirtualHost *:80> php_admin_value display_errors Off </VirtualHost> ``` --- #### 二、运行时动态修改 1. **通过`ini_set()`函数** 在PHP代码中临时修改(需脚本有执行权限): ```php ini_set('display_errors', '0'); // 关闭错误显示 error_reporting(E_ALL); // 设置错误报告级别 ini_set('log_errors', '1'); // 开启日志记录 ini_set('error_log', '/path/custom_error.log'); ``` 2. **`.htaccess`文件覆盖** 在网站根目录的`.htaccess`中添加: ```apache php_flag display_errors Off php_value error_log /var/log/php_errors.log ``` --- #### 三、配置失效的常见原因(引用[2][3][4]) | 问题现象 | 原因分析 | 解决方案 | |------------------------|--------------------------------------------------------------------------|--------------------------------------------------------------------------| | `display_errors=Off`未生效 | 1. `log_errors=On`但未设置`error_log`或文件不可写[^2] | 检查`error_log`路径权限,或直接设置`log_errors=Off` | | | 2. 代码中使用`ini_set()`或`set_error_handler()`覆盖了配置[^4] | 检查代码中是否动态修改了错误处理逻辑 | | | 3. PHP-FPM子进程未重载配置(如修改后未执行`service php-fpm reload`)[^1] | 重启PHP服务或使用`opcache_reset()`清理缓存 | | | 4. 存在多个`php.ini`文件(如cli和fpm配置分离) | 通过`phpinfo()`确认当前生效的配置文件 | --- #### 四、生产环境推荐配置(引用[3]) ```ini ; 完全关闭错误显示 display_errors = Off ; 记录所有错误(排除提示类错误) error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; 启用错误日志并指定路径 log_errors = On error_log = /var/log/php_errors.log ; 禁止HTML格式错误输出 html_errors = Off ``` --- #### 五、验证配置生效 1. **检查当前配置** ```php <?php echo "display_errors: " . ini_get('display_errors') . "\n"; echo "error_log: " . ini_get('error_log'); ?> ``` 2. **触发测试错误** ```php <?php trigger_error("Test error message", E_USER_WARNING); ``` - 成功特征:页面不显示错误,但日志文件`/var/log/php_errors.log`中会记录警告信息[^3] --- ### 关键总结 1. **配置文件优先级**: `.htaccess`/`.user.ini` > 虚拟主机配置 > `php.ini` 2. **防御性配置**: - 生产环境**必须关闭**`display_errors`,避免泄露敏感信息 - 通过`error_log`集中管理错误日志,定期审计 3. **代码审计要点**: 检查是否存在`error_reporting(0)`或`@`运算符等干扰配置的行为[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值