PHP Error Checking

Source: Internet
Author: User
Tags openlog php error

For PHP developers, once a product is put into use, the display_errors option should be turned off immediately to avoid hackers being hacked by the path, database connection, data table, and other information that these errors reveal. However, when any product is put into use, there will inevitably be errors, so how to record some useful error reports to developers? We can use the error report as a log in a separate text file. Logging of error logs can help developers or managers see if there is a problem with the system. If you need to write the error report in the program to the error log, simply open the configuration command log_errors in the PHP configuration file. Error reporting is logged to the Web server's log file by default, such as in the error log file Error.log of the Apache server. Of course, the error log can be logged to the specified file or sent to Syslog, respectively, described as follows:

1. Log error reports using the specified file

Use the specified file to record the error report log using the specified file record error reporting log using the specified file Logging error report log if you log the error logs using the files you specify, be sure to keep this file outside the document root to reduce the likelihood of attack. And the file must have write permissions for the executing user of the PHP script (the Web server process owner). 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 has write permissions. Then in the PHP configuration file, set the value of the error_log instruction to the absolute path of this error log file.

The configuration directives in php.ini need to be modified as follows:

1. error_reporting = E_all; Each error that occurs will be reported to PHP

2. display_errors = OFF; do not display all error reports that meet the rules defined by the previous instruction

3. Log_errors = on; Determines the location of log statement records

4. Log_errors_max_len = 1024; Sets the maximum length for each log entry

5. Error_log =/usr/local/error.log; Specifies the log file location where the resulting error report is written

The PHP configuration file is set up as above and restarts the Web server. Thus, 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 that you specified. In addition, you can not only record all errors that satisfy the rules defined by error_reporting, but also use the Error_log () function in PHP to send a user-defined error message.

The prototype of the function is as follows:

1. bool Error_log (String message [, int message_type [, String destination [, String extra_headers]])

This function sends an error message to the Web server's error log file, to a TCP server, or to the specified file. Returns True if the function executes successfully, or false if it fails. The first parameter, message, is a required option, which is the error message to send. If you use only this parameter, messages are sent at the location set in profile php.ini. The second parameter message_type is the integer value: 0 is sent to the operating system log, 1 uses the PHP mail () function, sends the message to an e-mail, the fourth parameter extra_headers also uses; 2 sends the error message to the TCP server. At this point the third parameter, destination, indicates that the destination IP and port;3 the information to the file destination.

If you take the problem of logging in to an 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 database is not available!", 0); To write an error message to the operating system log

4.}

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

6. Error_log ("Big Trouble!", 1, ".)   MyDomain.com "); Send to admin mailbox

7.}

8. Error_log ("Screwed Up!", 2, "localhost:5000"); Sent to a server that corresponds to a 5000 port on this machine

9. Error_log ("Screwed Up!", 3, "/usr/local/errors.log"); Sent to the specified file

Ten.?>

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

Error messages are logged in the operating system log, error messages are logged in the operating system log, error messages are logged in the operating system log, and error reports can be recorded in the operating system log, but the log management between different operating systems is a little differently. On Linux The error statement will be sent to Syslog, and the error on Windows will be sent to the event log. If you are unfamiliar with syslog, be aware 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, which can often be viewed through the Event Viewer. If you want to write the error report to the operating system log, you can set the value of the Error_log directive to syslog in the configuration file.

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

1. error_reporting = E_all; Each error that occurs will be reported to PHP

2. display_errors = OFF; do not display all error reports that meet the rules defined by the previous instruction

3. Log_errors = on; Determines the location of log statement records

4. Log_errors_max_len = 1024; Sets the maximum length for each log entry

5. Error_log = syslog; Specifies that the resulting error report is written to the operating system log

In addition to the general error output, PHP also allows custom messages to be sent to syslog in the system. Although a custom message can be sent to the syslog using the Error_log () function described previously, there are 4 specialized functions that need to be used in PHP for this feature.

The following are presented separately:

Define_syslog_variables ()

This function must be called before using Openlog (), Syslog, and Closelog () three functions. Since this function is called, it will use some of the required constants at the beginning of the following three functions based on the current system environment.

Openlog ()

Opens a connection to the logger in the current system, ready to insert log messages into the system. and inserts the first string argument provided into each log message, the function also needs to specify two parameters that will be used in the log context, which can be referenced in the official documentation.

Syslog ()

This function sends a custom message to the system log. Requires two required parameters, and the first parameter customizes the priority of the message by specifying a constant. For example, log_warning represents a general warning, Log_emerg represents a serious problem that can portend a system crash, and some other constants that represent severity can be referenced in official documentation. The second parameter is a custom message sent to the system log, either a message string or an error string provided by the PHP engine at run time.

Closelog ()

This function is called after it has been sent to the system log to complete a custom message, closing the log connection opened by the Openlog () function.

If the instructions for sending a custom message to the syslog have been turned on in the configuration file, you can send a warning message to the system log using the four functions described earlier, and through the Syslog parsing tool in the system, view and analyze the custom messages sent by the PHP program as follows:

1.

2. Define_syslog_variables ();

3. Openlog ("PHP5", Log_pid, Log_user);

4. Syslog (log_warning, "warning report sent to syslog for demo, warning Time:". Date ("Y/m/d h:i:s"));

5. Closelog ();

6.?>

Take Windows system as an example, you can see our own custom warning message by right-clicking on my Computer and selecting Manage options, then selecting Event Viewer from the System Tools menu, and then finding the application options. The above code will generate a message similar to the following in the syslog file of the system, which is part of the event:

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

The error log is logged using the specified file or syslog, depending on your Web server environment. If you can control the Web server, using Syslog is ideal because you can use the parsing tools of the syslog to view and analyze the logs. But if your site is running on a shared server's virtual host, you only have to use a separate text file to record the error log.

PHP Error Checking

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.