PHP error handling method (development and launch)

Source: Internet
Author: User
Tags openlog server error log
For PHP developers, once a product is put into use, the display_errors option should be disabled immediately, this prevents hackers from attacking the paths, database connections, data tables, and other information exposed by these errors. However, when any product is put into use, it is difficult to avoid errors. how can we record some useful error reports for developers? We can use the error report as a log record in a separate text file. The error log record can help developers or administrators check whether there is a problem in the system. If you need to write the error report in the program into the error log, you only need to enable the configuration command log_errors in the PHP configuration file. By default, error reports are recorded in the Web server log file, for example, error. log in the Apache server error log file. Of course, you can also record the error log to the specified file or send it to the system syslog, as described below:

1. use the specified file to record 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 your specified file to record the error log, make sure that this file is stored outside the document root directory to reduce the possibility of attacks. In addition, the PHP script execution user (Web server process owner) must have the write permission for this file. 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 has the write permission. In the PHP configuration file, set the error_log command value to the absolute path of the error log file.

Modify the configuration commands in php. ini as follows:

1. error_reporting = E_ALL; every error reported to PHP

2. display_errors = Off; all error reports that meet the rules defined in the preceding command are not displayed.

3. log_errors = On; determines the location of log statement Records

4. log_errors_max_len = 1024; set the maximum length of each log item

5. error_log =/usr/local/error. log; specify the location of the log file written in the generated error report

After setting the PHP configuration file in the preceding way, restart the Web server. In this way, all error reports generated when executing any PHP script file will not be displayed in the browser, logs are recorded in the specified error log/usr/local/error. log. In addition, you can not only record all errors that meet the rules defined by error_reporting, but also use the error_log () function in PHP to send a custom error message.

The function is prototype as follows:

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

This function sends an error message to the error log file of the Web server, a TCP server, or a specified file. If the function is successfully executed, TRUE is returned. if the function fails, FALSE is returned. The first parameter message is required, that is, the error message to be sent. If you only use this parameter, a message is sent at the location set in the configuration file php. ini. The second parameter message_type is an integer. 0 indicates that the message is sent to the log of the operating system. 1. use the Mail () function of PHP to send the message to an e-mail, the fourth parameter extra_headers is also used. 2. the error message is sent to the TCP server. The third parameter destination indicates the destination IP address and Port. 3. the information is saved to the file destination.

If the problem occurs when you log on to the Oracle database as an example, the function is used as follows:

1.

2. if (! Ora_Logon ($ username, $ password )){

3. error_log ("Oracle database unavailable! ", 0); // write the error message to the operating system log

4 .}

5. if (! ($ Foo = allocate_new_foo ()){

6. error_log ("It's a big headache! ", 1,". mydomain.com "); // send it to the administrator's mailbox

7 .}

8. error_log ("screwed up! ", 2," localhost: 5000 "); // send it to the server corresponding to port 5000 on the local machine

9. error_log ("screwed up! ", 3,"/usr/local/errors. log "); // send to the specified file

10.?>

2. the error message is recorded in the operating system log.

The error information is recorded in the operating system logs. the error information is recorded in the operating system logs. the error report can also be recorded in the operating system logs, however, log management varies with operating systems. In Linux, the error statement will be sent to syslog, and in Windows, the error will be sent to the event log. If you are not familiar with syslog, you should at least know that it is a UNIX-based log tool. It provides an API to record messages related to system and application execution. Windows event logs are actually the same as UNIX syslog logs, which can be viewed in the event viewer. If you want to write the error report to the operating system log, you can set the error_log command value to syslog in the configuration file.

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

1. error_reporting = E_ALL; every error reported to PHP

2. display_errors = Off; all error reports that meet the rules defined in the preceding command are not displayed.

3. log_errors = On; determines the location of log statement Records

4. log_errors_max_len = 1024; set the maximum length of each log item

5. error_log = syslog; specify to write the generated error report to the operating system log.

In addition to common error output, PHP allows custom messages to be sent to the system syslog. Although the error_log () function described earlier can also send custom messages to syslog, four dedicated functions are provided for this feature in PHP.

They are described as follows:

Define_syslog_variables ()

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

Openlog ()

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

Syslog ()

This function sends a custom message to the system log. Two required parameters are required. The first parameter customizes the priority of a message by specifying a constant. For example, LOG_WARNING indicates a general warning, and LOG_EMERG indicates that the system crashes seriously. for some other constants that indicate the severity, refer to the official documentation. The second parameter is a custom message sent to the system log. it must provide a message string or an error string provided by the PHP engine at runtime.

Closelog ()

This function is called after a custom message is sent to the system log to close the log connection opened by the openlog () function.

If you have enabled the command to send custom messages to syslog in the configuration file, you can use the four functions described earlier to send a warning message to the system log, you can use the syslog parsing tool in the system to view and analyze custom messages sent by the PHP program, as shown below:

1.

2. define_syslog_variables ();

3. openlog ("PHP5", LOG_PID, LOG_USER );

4. syslog (LOG_WARNING, "demonstration of the warning report sent to syslog, warning time:". date ("Y/m/d H: I: s "));

5. closelog ();

6.?>

Take Windows as an example. right-click "My Computer" and select the management option. then, go to the System Tools menu, select the Event Viewer, and find the application options, we can see our own custom warning messages. The above code will generate a message similar to the following in the system syslog file, which is part of the event:

1. PHP5 [3084], warning report for the demonstration sent to syslog, warning time: 2009/03/26 04:09:11.

Whether to use the specified file or syslog to record error logs depends on your Web server environment. If you can control the Web server, syslog is the best, because you can use the syslog parsing tool to view and analyze logs. However, if your website runs on the virtual host of the shared server, you only need to use a separate text file to record error logs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.