Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

420 Technique 54: Timesaving PHP Tricks

Sending E-Mail from PHP When Problems Occur

The PHP script described in the previous section works well when conditions are perfect. We omitted error-checking code that you would normally include in a real-world application. Quite a few things can go wrong while checking for weather conditions: The server (ejse.com) could be down, the zip code you’ve entered may be incorrect, or the XML stream may contain invalid data. Checking for errors is important if you want your Web site to be robust and well-behaved.

If you don’t do anything special to intercept error messages, they are displayed in the Web browser. That’s not a very nice way to treat your users because PHP error messages are often detailed, a bit abrasive, and of little use to the user.

Given that PHP programs usually run within a Web server, you don’t have too many options when something goes wrong. You’re not sitting in front of a PHP debugger, so you can’t step through the program, checking return codes and variables as the script runs. The most obvious way to find out what’s gone wrong is to send error messages to the browser, but that’s not very friendly (and the chances of a Web user writing down all the details that you need are slim). You could write messages to a debug log, but then you’d have to remember to look through those logs occasionally to check for problems.

What you really want is a PHP program that actively notifies you by e-mail when something goes wrong.

Listing 54-4 shows a short PHP function that will intercept errors, warnings, and informational messages and send an e-mail describing the problem to the recipient of your choice.

my_err_handler() doesn’t do anything without a little help. To enable my_err_handler(), call PHP’s set_error_handler() function, giving it the name of the function that you want to run whenever an error (or warning) occurs, like this:

set_error_handler( “my_err_handler” );

All PHP error handler functions are called with the same five arguments:

The first argument indicates the severity of the problem encountered. $severity will be one of the following values:

E_USER_ERROR

E_USER_WARNING

E_USER_NOTICE

E_WARNING

E_NOTICE

The second argument ($errmsg) contains the text of the error message.

The third and fourth arguments ($filename and $lineno) indicate the point at which the error occurred.

The last argument is an array that contains the variable values and variable names in the function in which the error occurred.

You can often resolve a problem given only the argument values provided to the error handler function, but on occasion, you need more details. The my_err_handler() function in Listing 54-4 e-mails you the error message, filename, and line number, but Listing 54-5 shows a new version of my_err_ handler() that gives much more detail. Use the new version while you’re debugging your PHP code and the original version when things seem to be humming along tickety boo (that’s a technical term).

Sending E-Mail from PHP When Problems Occur 421

LISTING 54-4: MAILING ERROR MESSAGES FROM PHP

function my_err_handler( $severity, $errmsg, $filename, $lineno, $context )

{

$err_text

=

“At “ . date(“Y-m-d H:I:s (T)”);

$err_text .=

an error occurred at line “ . $lineno;

$err_text .=

of

file “ . $filename . “\n\n”;

$err_text .=

“The

text of the error message is:\n”;

$err_text

.=

$errmsg . “\n”;

mail( “my-email@example.com”, “PHP Script Error”, $err_txt );

LISTING 54-5: MAILING DETAILED ERROR MESSAGES FROM PHP

function my_err_handler( $severity, $errmsg, $filename, $lineno, $context )

{

$err_text =

“At “ . date(“Y-m-d H:I:s (T)”);

$err_text .=

“ an error occurred at line “ . $lineno;

$err_text .=

“ of file “ . $filename . “\n\n”;

$err_text .=

“The text of the error message

is:\n”;

$err_text .=

$errmsg . “\n”;

 

 

ob_start();

 

// Redirect

output to the temporary buffer

print_r( debug_backtrace());

// Generate

(and format) a stack trace

$err_text .=

“Stack Trace follows:\n”;

 

$err_text .= ob_get_contents(); // Add the stack trace to the body of the e-mail message

ob_end_clean(); // And clean out the temporary buffer

mail( “my-email@example.com”, “PHP Script Error”, $err_txt );

}

55

Using the DDD

 

 

Graphical Debugger

Technique

with Perl

 

Save Time By

Using DDD to debug Perl programs

Using the data window to track variable values

Watching your code with breakpoints

Using the Backtrace dialog to view the contents of your stack

Perl is often referred to as the duct tape of the Internet. If you need to write a utility or script, Perl is a standard that you can count on. How can you save time when you use Perl? With a good debugger!

Unless your code works perfectly the first time through, you’ll be spending some time behind the window of a debugger, working the kinks out of your script.

Linux offers a great open-source debugger called DDD (data display debugger). DDD is a graphical front end for several different command line debuggers and saves you time by clarifying and simplifying the process of debugging programs. DDD can debug not only Perl code, but also C, C++, Java, and other programming languages.

You can use the source window in DDD to review your source code at a glance; you can even define and delete breakpoints and see cute little stop signs where your program will stop. DDD also provides an easy button interface to step through your program, stepping into subroutines to view the complete code or bypassing subroutines.

With DDD, you can create a graphical display of the variables in your program and watch their values change as you step through the program. DDD dialogs help you in your debugging tasks by providing easy-to-read information about the status of your program. An easy-to-access help menu makes certain you have not only technical information, but also advice about debugging your program (in case you get stuck).

A lot of good books about the Perl programming language are available. The best timesaver we could think of was a quick introduction to an open-source debugger that will save you time and make your life a lot easier. In this technique, we introduce you to DDD, which is a huge improvement over the command line debuggers of the past.

Соседние файлы в предмете Операционные системы