Posts Tagged ‘error’

Hiding Error Messages from Users

Friday, March 7th, 2008

Set the following values in your php.ini or web server configuration file:
display_errors =off
log_errors =on
These settings tell PHP not to display errors as HTML to the browser but to put
them in the server’s error log.
When log_errors is set to on,error messages are written to the server’s error log. If
you want PHP errors to be written to a separate file,set the error_log configuration
directive with the name of that file:
error_log = /var/log/php.error.log

(more…)

Using a Custom Error Handler

Wednesday, March 5th, 2008

You want to create a custom error handler that lets you control how PHP reports errors.
Solution
To set up your own error function, use set_error_handler():

set_error_handler('pc_error_handler');
function pc_error_handler($errno, $error, $file, $line) {
$message = "[ERROR][$errno][$error][$file:$line]";
error_log($message);
}

Discussion

A custom error handling function can parse errors based on their type and take the appropriate action. See Table 8-2 in Recipe 8.15 for a list of error types. Pass set_error_handler() the name of a function,and PHP forwards all errors to that function. The error handling function can take up to five parameters. The first parameter is the error type,such as 8 for E_NOTICE. The second is the message thrown by the error,such as “Undefined variable: html”. The third and fourth arguments are the name of the file and the line number in which PHP detected the error. The final parameter is an array holding all the variables defined in the current scope and their values.
For example,in this code $html is appended to without first being assigned an initial value: (more…)