Logging PHP Errors


In the previous example, errors were handled by simply printing them out in detail or not. Another option is to log the errors: make a permanent note of them somehow. For this purpose, the error_log() function instructs PHP how to file an error.

 error_log (message, type, destination,   The message value should be the text of the logged error. The type dictates how the error is logged. Table 6.2 lists the types of error logging you can use. The destination parameter can be either the name of a file (for log type 3) or an email address (for log type 1). The extra headers argument is used only when sending emails (log type 1). Both the destination and extra headers are optional.

Table 6.2. PHP can log errors using these four techniques.

Error Log Types

NUMBER

MEANING

0

The message is logged using the operating system's default method.

1

The message is sent by email to the destination address.

2

The message is sent to a remote debugger (for example, another server).

3

The message is recorded in a text file.


With this in mind, the error handling function will be modified so that it can email the errors that occur to you, the site's administrator.

Using die() and exit()

In Chapter 3, "Creating Dynamic Web Sites," the continue and break statements were used to control the execution of a script, specifically within control structures. For example, continue will halt the current execution of a loop and move on to the next iteration. Or the break statement can be used to exit loops and switch conditionals. Similar to these are die() and exit(), two synonymous functions (or language constructs, technically).

When a die() or exit() is called in your script, the entire script is terminated. These are useful for stopping a script from continuing should something importantlike establishing a database connectionfail to happen. You can also pass die() and exit() a string that will be printed out in the browser.

You'll commonly see die() or exit() used in an OR conditional. For example:

 include('config.inc.php') OR die ('Could not open the file.'); 

With a line like that, if PHP could not include the configuration file, the die() statement will be executed and the Could not open the file. message will be printed. You'll see variations on this throughout this book and in the PHP manual, as it's a quick (but potentially excessive) way to handle errors without using a custom error handler.


To use error logging

1.

Open handle.inc.php (refer to Script 6.2) in your text editor.

2.

After declaring the $live variable, define an email address (Script 6.4).

 $email = 'me@address.com'; 

Script 6.4. This modified version of the error handling file sends emails when errors occur.


The email address should be yours, where the errors that occur should be sent.

3.

Change the global line inside of the function so that the email address is available.

 global $live, $email; 

In order to use the email address, it must also be global.

4.

If the site is live, also send the error message to an email address.

 error_log ($message, 1, $email); 

This function will send the error message to that email address.

5.

Save the file and upload it to the same directory as error.php.

6.

Rerun error.php with the $live variable equal to TRUE.

7.

Check your email to see the error messages (Figures 6.11 and 6.12 on next page).

Figure 6.11. While the user stills sees a simple message in the browser, a detailed error message is emailed to the system administrator.


Figure 6.12. This is the second email, for the second error that occurred. Notice that the list of variables will differ from server to server (compare with those in Figure 6.9).




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

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