Error handling is an important aspect of site development but using PHP there aren't too many clearly defined ways to handle errors. I will present a simple function that handles errors in several ways: records the error in a log, emails the site administrator a copy of the error, and redirects the user to a central error page. Using one or more of these three methods, you can handle errors more gracefully in your PHP pages.

The PHP Functions

PHP error handling is described at showing all the different functions available. What I tried to do was take a few of the most useful ways of handling errors and put them into one function. When I first implement a site, I like to have errors emailed to me. As a site becomes fully debugged, I like to keep track of errors in a log and remove my email address. Finally, I like to shield the end user from the actual error and only show him a friendly page so that no server information is given out. This is also a way to keep hackers from knowing too much about your site, as a hacker will intentionally try to cause errors to find things out.

We will use several basic PHP functions in this custom function: mail() to mail the errors, error_log() to log the errors, set_error_handler() to set our custom function, ini_set() to turn error display on during debugging, and error_reporting() to set the error reporting level.

The Custom Functions

We'll use a custom function called errorHandler(). At the top of the function, we can set a few variables:

The function is shown below:

/* Handle PHP errors gracefully and provide logs and/or emails */

function handle_error ($errno, $errstr, $errfile, $errline, $vars){
  /* Set the following line to a default error page*/
  $errorPage = "error.php";
  /* Set the following line to a default location in your server
    to log all errors or leave blank for no logging */
  $errorLog = "c:\errors.txt"; // Actual path -- this works on Windows
  /* Set the following line to an email address for the
    errors to be sent, or leave blank for no notification*/
  $errorEmail = "";
  /* Set the following variable to true to pass error message in query
    string to error page. SET TO false FOR LIVE SITE*/
  $errorShowErrors = false;

  // Set the time of the error
  $errorTime = date("Y-m-d H:i:s");
  // Set the query string, if any
  $errorQuerystring = ($errorShowErrors) ? "?error=" . urlencode($errstr) : "";
  $errorMessage = "$errstr in $errfile on line $errline at $errorTime\r\n";
  if($errorLog != "")
    error_log($errorMessage, 3, $errorLog);
  if($errorEmail != "") {
    $errorHeader = "From: " . $errorEmail;
    mail($errorEmail, "Error at $errfile", $errorMessage, $errorHeader);
  header("Location: $errorPage$errorQuerystring");


Using the Function

To use the function simply include the errorHandler.php file on your PHP page, or in a central page that is available to all pages in your site (see my article on implementing an application.php file for such a file). I like to include the file with a conditional so that the error handling itself can be turned on and off by the site administrator. Add this code to a test.php page:

<?php // Turn error handling on or off
$errorHandling = true;

if($errorHandling) {
  // Set Custom error page to be shown
  //turn on all error messages
  ini_set("display_errors", "on");

The error handling page is conditionally included if you set the variable $errorHandling to true. If set to false, we use ini_set() to turn regular PHP error messages (page error messages) on and set reporting to E_ALL, which includes all errors but not warnings. Set this to E_STRICT to show warnings as well. If this $errorHandling variable is set to true, as shown here, the errorHandling() function will kick in whenever an error occurs, and depending on your settings in that file, you will either have an email, an error log entry, or both. The $errorHandling variable here can be replaced with a url variable, session variable, or other setting that you prefer within your site. I typically use a session variable pulled from a settings table in the database.

We'll test it out on a test page. First, make sure you have the error.php file in place. This is the default error page that simply displays a friendly message to the user. Next, make sure the errorHandler.php file is in place as well; this is required for the error handling. Finally, add this line on the test.php page, which will cause an intentional error:

<?php $test=$badvariable;?>

Since $badvariable is undefined, it will always cause an error. Our error handler should pick this up, writing a line to the log file and sending an email to the address declared in the error handler:

Undefined variable: badvariable in D:\webroot\cmxstaging\source\phperrors\test.php on line 11 at 2007-09-23 15:19:13


Error handling is tricky but can be done pretty easily in PHP with a few built-in functions and a little coding.