Tải bản đầy đủ (.pdf) (5 trang)

PHP and MySQL Web Development - P100 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (106.85 KB, 5 trang )

467
Altering the Error Reporting Settings
Altering the Error Reporting Settings
You can set the error reporting settings globally, in your php.ini file or on a per script
basis.
To alter the error reporting for all scripts, you can modify these four lines in the
default php.ini file:
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
log_errors = Off
track_errors = Off
The default global settings are to
n
report all errors except notices
n
output error messages as HTML to standard output
n
not log error messages to disk
n
not track errors, storing the error in the variable $php_errormsg
The most likely change you are to make is to turn the error reporting level up to E_ALL.
This will result in many notices being reported, for incidents that might indicate an
error, or might just result from the programmer taking advantage of PHP’s weakly typed
nature and the fact that it automatically initializes variables to 0.
While debugging, you might find it useful to set the error_reporting level higher.
In production code, if you are providing useful error messages of your own, it would be
more professional looking to turn display_errors off and to turn log_errors on,
while leaving the error_reporting level high.You will then be able to refer to detailed
errors in the logs if problems are reported.
Tu r ning track_errors on might help you to deal with errors in your own code,
rather than letting PHP provide its default functionality. Although PHP provides useful


error messages, its default behavior looks ugly when things go wrong.
By default, when a fatal error occurs, PHP will output the following:
<br>
<b>Error Type</b>: error message in <b>path/file.php</b>
on line <b>lineNumber</b><br>
and stop executing the script. For nonfatal errors, the same text is output, but execution
is allowed to continue.
This HTML output makes the error stand out, but looks poor.The style of the error
message is unlikely to fit the rest of the site’s look. It might also result in Netscape users
seeing no output at all if the page’s content is being displayed within a table.This is
because HTML that opens but does not close table elements, such as
<table>
<tr><td>
29 525x ch23 1/24/03 2:56 PM Page 467
468
Chapter 23 Debugging
<br>
<b>Error Type</b>: error message in <b>path/file.php</b>
on line <b>lineNumber</b><br>
will be rendered as a blank screen by Netscape.
We do not have to keep PHP’s default error handling behavior, or even use the same
settings for all files.To change the error reporting level for the current script, you can call
the function error_reporting().
Passing an error report constant, or a combination of them, sets the level in the same
way that the similar directive in php.ini does.The function returns the previous error
reporting level.A common way to use the function is like this:
// turn off error reporting
$old_level = error_reporting(0);
// here, put code that will generate warnings
// turn error reporting back on

error_reporting($old_level);
This code snippet will turn off error reporting, allowing us to execute some code that is
likely to generate warnings that we do not want to see.
Tu r ning off error reporting permanently is a bad idea as it makes it difficult to find
your coding errors and fix them.
Tr iggering Your Own Errors
The function trigger_error()can be used to trigger your own errors. Errors created in
this way will be handled in the same way as regular PHP errors.
The function requires an error message, and can optionally be given an error type.
The error type needs to be one of E_USER_ERROR, E_USER_WARNING, or E_USER_NOTICE.
If you do not specify a type, the default is E_USER_NOTICE.
You use trigger_error() as shown in the following:
trigger_error('This computer will self destruct in 15 seconds', E_USER_WARNING);
(Note that this function was added at PHP version 4.0.1.)
Handling Errors Gracefully
If you come from a C++ or Java background, you might miss exception handling when
you use PHP. Exceptions allow functions to signal that an error has occurred and leave
dealing with the error to an exception handler.Although PHP does not have exceptions,
PHP 4.0.1 introduced a mechanism that can be used in a similar way.
You have already seen that you can trigger your own errors.You can also provide
your own error handlers to catch errors.
The function set_error_handler() lets you provide a function to be called when
user-level errors, warnings, and notices occur.You call set_error_handler() with the
name of the function you want to use as your error handler.
29 525x ch23 1/24/03 2:56 PM Page 468
469
Handling Errors Gracefully
Your error handling function must take two parameters: an error type and an error
message. Based on these two variables, your function can decide how to handle the
error.The error type must be one of the defined error type constants.The error message

is a descriptive string.
A call to set_error_handler() will look like this:
set_error_handler('myErrorHandler');
Having told PHP to use a function called myErrorHandler(),we must then provide a
function with that name.This function must have the following prototype:
myErrorHandler(int error_type, string error_msg)
but what it actually does is up to you.
Logical actions might include
n
Displaying the error message provided
n
Storing information in a log file
n
Emailing the error to an address
n
Te r minating the script with a call to exit
Listing 23.2 contains a script that declares an error handler, sets the error handler using
set_error_handler(), and then generates some errors.
Listing 23.2 handle.php—This Script Declares a Custom Error Handler and Generates
Different Errors
<?php
// The error handler function
function myErrorHandler ($errno, $errstr, $errfile, $errline)
{
echo "<br /><table bgcolor='#cccccc'><tr><td>
<p><b>ERROR:</b> $errstr</p>
<p>Please try again, or contact us and tell us that
the error occurred in line $errline of file '$errfile'</p>";
if ($errno == E_USER_ERROR||$errno == E_ERROR)
{

echo '<p>This error was fatal, program ending</p>';
echo '</td></tr></table>';
//close open resources, include page footer, etc
exit;
}
echo '</td></tr></table>';
}
// Set the error handler
set_error_handler('myErrorHandler');
29 525x ch23 1/24/03 2:56 PM Page 469
470
Chapter 23 Debugging
//trigger different levels of error
trigger_error('Trigger function called', E_USER_NOTICE);
fopen('nofile', 'r');
trigger_error('This computer is beige', E_USER_WARNING);
include ('nofile');
trigger_error('This computer will self destruct in 15 seconds', E_USER_ERROR);
?>
The output from this script is shown in Figure 23.1.
Listing 23.2 Continued
Figure 23.1 You can give friendlier error messages
than PHP if you use your own error handler.
This custom error handler does not do any more than the default behavior. Because this
code is written by you, you can make it do anything. It gives you a choice about what
to tell your visitors when something goes wrong and how to present that information so
that it fits the rest of the site. More importantly, it gives you flexibility to decide what
happens. Should the script continue? Should a message be logged or displayed? Should
tech support be alerted automatically?
It is important to note that your error handler will not have the responsibility for

dealing with all error types. Some errors, such as parse errors and fatal runtime errors
will still trigger the default behavior. If this concerns you, make sure that you check
29 525x ch23 1/24/03 2:56 PM Page 470
471
Next
parameters carefully before passing them to a function that can generate fatal errors and
trigger your own E_USER_ERROR level error if your parameters are going to cause
failure.
Next
In Chapter 24,“Building User Authentication and Personalization,” we will begin our
first project. In this project, we’ll look at how you can recognize users who are coming
back to your site and tailor your content appropriately.
29 525x ch23 1/24/03 2:56 PM Page 471

×