Home >Backend Development >PHP Tutorial >Detailed introduction to php error checking

Detailed introduction to php error checking

黄舟
黄舟Original
2017-10-10 10:13:592251browse

For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid being attacked by hackers due to the paths, database connections, data tables and other information disclosed by these errors. However, after any product is put into use, errors will inevitably occur, so how to record some error reports that are useful to developers? We can log error reports in separate text files. The recording of error logs can help developers or managers check whether there are problems with the system. If you need to write the error report in the program to the error log, just turn on the configuration directive log_errors in the PHP configuration file. Error reports will be logged to the log file of the web server by default, for example, to the error log file error.log of the Apache server. Of course, you can also record the error log to a specified file or send it to the system syslog. The details are as follows:

1. Use the specified file to record the error report log

Use The specified file records the error report log. Use the specified file to record the error report log. Use the specified file to record the error report log. If you use a specified file to record the error log, be sure to store this file outside the document root directory to reduce errors. to the possibility of attack. And the file must be written by the user who executes the PHP script (the owner of the Web server process). Assume that in the Linux operating system, the error.log file in the /usr/local/ directory is used as the error log file, and the Web server process user is set to have write permissions. Then in the PHP configuration file, set the value of the error_log directive to the absolute path of the error log file.

You need to modify the configuration instructions in php.ini as follows:

1. error_reporting  =  E_ALL                   ;将会向PHP报告发生的每个错误   
2. display_errors = Off                        ;不显示满足上条 指令所定义规则的所有错误报告   
3. log_errors = On                             ;决定日志语句记录的位置   
4. log_errors_max_len = 1024                   ;设置每个日志项的最大长度   
5. error_log = /usr/local/error.log                ;指定产生的 错误报告写入的日志文件位置

After the PHP configuration file is set as above, and restart the web server. In this way, when executing any PHP script file, all error reports generated will not be displayed in the browser, but will be recorded in the error log /usr/local/error.log specified by you. In addition, not only can all errors that meet the rules defined by error_reporting be recorded, but also a user-defined error message can be sent using the error_log() function in PHP.

The prototype of this function is as follows:

 1. bool error_log ( string message [, int message_type  [, string destination [, string extra_headers]]] )

This function will send error information to the error log file of the Web server, a TCP server or to a specified file. This function returns TRUE if successful and FALSE if failed. The first parameter message is required, which is the error message to be sent. If only this parameter is used, the message will be sent at the location set in the configuration file php.ini. The second parameter message_type is an integer value: 0 means sending it to the log of the operating system; 1 uses PHP's Mail() function to send the message to an E-mail address, and the fourth parameter extra_headers will also be used; 2 Send the error message to the TCP server. At this time, the third parameter destination represents the destination IP and Port; 3. Save the information to the file destination.

If we take the problem of logging into the Oracle database as an example, the use of this function is as follows:

1. <?php      
2.     if(!Ora_Logon($username, $password)){     
 3.         error_log("Oracle数据库不可用!", 0);        //将错误消息写入到操作系统日志中   
4.     }   
5.     if(!($foo=allocate_new_foo()){   
6.         error_log("出现*烦了!", 1, ". mydomain.com");   //发送到管理员邮箱中   
7.     }  
8.     error_log("搞砸了!",   2,   "localhost:5000");     //发送到本机对应5000端口的服务器中   
9.     error_log("搞砸了!",   3,   "/usr/local/errors.log");  //发送到指定的文件中   
10. ?>

2. The error information is recorded in the log of the operating system.

Error information is recorded in the operating system log. Error information is recorded in the operating system log. Error information is recorded in the operating system log. Error reports can also be recorded in the operating system log, but it is different. Log management differs somewhat between operating systems. On Linux error statements will be sent to syslog, while on Windows errors will be sent to the event log. If you are not familiar with syslog, at least know that it is a UNIX-based logging tool that provides an API to record messages related to system and application execution. The Windows event log is actually the same as the UNIX syslog, and these logs can usually be viewed through Event Viewer. If you want to write error reports to the operating system log, you can set the value of the error_log directive to syslog in the configuration file.

The specific configuration instructions that need to be modified in php.ini are as follows:

1. error_reporting  =  E_ALL                   ;将会向PHP报告发生的每个错误   
2. display_errors = Off                            ;不显示 满足上条指令所定义规则的所有错误报告   
3. log_errors = On                             ;决定日志语句记录的位置   
4. log_errors_max_len = 1024                   ;设置每个日志项的最大长度   
5. error_log = syslog                          ;指定产生的错误报告写入操作系统的日志里

In addition to general error output, PHP also allows sending to the system syslog Customized messages. Although customized messages can also be sent to syslog through the error_log() function introduced earlier, PHP provides four dedicated functions for this feature that need to be used together.

They are introduced as follows:

define_syslog_variables()

You must call this function before using the three functions openlog(), syslog and closelog() function. Because when this function is called, it will initialize some necessary constants for the following three functions based on the current system environment.

openlog()

Open a connection to the logger in the current system to prepare for inserting log messages into the system. And insert the provided first string parameter into each log message. This function also needs to specify two parameters that will be used in the log context. You can refer to the official documentation for use.

syslog()

该 函数向系统日志中发送一个定制消息。需要两个必选参数,第一个参数通过指定一个常量定制消息的优先级。例如LOG_WARNING表示一般的警 告,LOG_EMERG表示严重地可以预示着系统崩溃的问题,一些其他的表示严重程度的常量可以参考官方文档使用。第二个参数则是向系统日志中发送的定制 消息,需要提供一个消息字符串,也可以是PHP引擎在运行时提供的错误字符串。 

closelog()

该函数在向系统日志中发送完成定制消息以后调用,关闭由openlog()函数打开的日志连接。 

 

如果在配置文件中,已经开启向syslog发送定制消息的指令,就可以使用前面介绍的四个函数发送一个警告消息到系统日志中,并通过系统中的syslog解析工具,查看和分析由PHP程序发送的定制消息,如下所示: 

1.  
2.     define_syslog_variables();   
3.     openlog("PHP5", LOG_PID , LOG_USER);   
4.     syslog(LOG_WARNING, "警告报告向syslog中发送的演示, 警告时间:".date("Y/m/d H:i:s"));  
5.     closelog();   
6. ?>

 以Windows系统为例,通过右击"我的电脑"选择管理选项,然后到系统工具菜单中,选择事件查看器,再找到应用程序选项,就可以看到我们自己定制的警告消息了。上面这段代码将在系统的syslog文件中,生成类似下面的一条信息,是事件的一部分: 

      1. PHP5[3084], 警告报告向syslog中发送的演示, 警告时间:2009/03/26 04:09:11.  

使 用指定的文件还是使用syslog记录错误日志,取决于你所在的Web服务器环境。如果你可以控制Web服务器,使用syslog是最理想的,因为你能利 用syslog的解析工具来查看和分析日志。但如果你的网站在共享服务器的虚拟主机中运行,就只有使用单独的文本文件记录错误日志了。

The above is the detailed content of Detailed introduction to php error checking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn