Error Reporting and Logging

I l @ ve RuBoard

PHP has fairly good support built into it for both error reporting and handling. While these two concepts won't help you minimize your errors, they will give you another way of learning from them.

The error_reporting() function in PHP is used to establish what type of errors PHP should report on. The line error_report (0); turns error reporting off entirely. Errors will still occur; you just won't hear about them anymore. Conversely the line error_reporting (E_ALL); will report on every error that occurs, which PHP does not necessarily do. (The PHP manual lists all the possible levels of error reporting that can be set.) You'll use this function in the last section of the chapter, Using the Die Statement.

The error_reporting() tells PHP which errors to report upon while the error_log() function instructs PHP on how to file away error occurrences.

 error_log ("message", "type",   "destination"); 

When something goes awry, you can have PHP leave you a note in a file (i.e., record the errors in a log) or even email you directly. Prudent use of the error_log() function will allow Webmasters and programmers to keep better tabs on what the Web site is doing. To have PHP email you when an error occurs, add this line to your code:

 error_log ("message", "1",   "php@DMCinsights.com"); 

Let's alter the email.php script from Chapter 13, Creating Web Applications, so that it makes a note in a file every time it cannot successfully send an email.

To use the error_log() function:

  1. Open email.php in your text editor (Script 14.3).

  2. After line 9, add (Script 14.4):

     error_log ("Unable to send an email to  $MailTo from $MailFrom at " . time() .  "\n", 3, "errors.txt"); 
    Script 14.3. This is the email.php script from Chapter 13, Creating Web Applications. Currently it has no error logging capability, which would make it more useful.

    graphics/14sc03.gif

    Every time the mail() function is unable to work, a simple message will be recorded to log file indicating that $MailFrom was unable to send a message to $MailTo at a particular time (established using the time() function). A savvy administrator could view this file and either follow up with the users who had trouble or determine when the mail() function stopped working properly.

    Script 14.4. The modified version of email.php uses the error_log() function to record any problems that occur in sending emails. You can use error_log() to email a message as well, although not if the problem is in sending mail!

    graphics/14sc04.gif

  3. Save your script as email.php and upload it to the server.

    Now you'll need to create a blank document called errors.txt for the error_log() function to write to.

  4. Create a new blank document in your text editor.

  5. Save your document as errors.txt and upload it to the server in the same directory as email.php.

  6. Set the permissions on errors.txt so that everyone has write privileges on it.

  7. Test email.php in your Web browser (Figure 14.5,14.6, and 14.7).

    Figure 14.5. I've rewritten the email.php page to log any problems it encounters. This will all occur unbeknownst to the user .

    graphics/14fig05.gif

    Figure 14.6. All the user sees when a problem occurs is the simple message above the form. However, the system administrator can check the error log for more information (Figure 14.7).

    graphics/14fig06.jpg

    Figure 14.7. When making an error log, you can have it record whatever information you think is appropriate. This log lists the $MailTo and $MailFrom values along with a timestamp.

    graphics/14fig07.gif

Tip

If your email application that the server uses to send emails is functioning properly, this script will not log any errors. If you want to see what the script would do if it couldn't send a message, change line 6 to:

 if (!mail($MailTo, $Subject, $Body,   "From: $MailFrom")) { 

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net