Skip navigation

Beginning PHP, Apache, MySQL Web Development

Chapter 8 Code - Handling and Avoiding Errors

Try It Out 1

error.php - Try It Out 1

<?php
$error_no = $_SERVER['QUERY_STRING'];

switch ($error_no)
{
     case 400:
          $error_output = "<h1>&quot;Bad Request&quot; Error Page - (Error Code
                400)</h1>";
          $error_output .= "The browser has made a Bad Request<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
                the system administrator";
          $error_output .= " if you feel this to be in error";
     break;
     case 401:
          $error_output = "<h1>&quot;Authorization Required&quot; Error Page -
                (Error Code 401)</h1>";
          $error_output .= "You have supplied the wrong information to access a
                secure area<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
                the system administrator";
          $error_output .= " if you feel this to be in error";
     break;
     case 403:
          $error_output = "<h1>&quot;Forbidden Access&quot; Error Page - (Error Code
                403)</h1>";
          $error_output .= "You are denied access to this area<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
                the system administrator";
          $error_output .= " if you feel this to be in error";
     break;
     case 404:
          $error_output = "<h1>&quot;Page Not Found&quot; Error Page - (Error Code
                404)</h1>";
          $error_output .= "The page you are looking for cannot be found<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
                the system administrator";
          $error_output .= " if you feel this to be in error";
     break;
     case 500:
          $error_output = "<h1>&quot;Internal Server Error&quot; Error Page - (Error Code
                500)</h1>";
          $error_output .= "The server has encountered an internal error<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
                the system administrator";
          $error_output .= " if you feel this to be in error";
     break;
     default:
          $error_output = "<h1>Error Page</h1>";
          $error_output .= "This is the custom error Page<br>";
          $error_output .= "You should be <a href=\"index.php\">here</a>";
}
?>
<html>
<head>
     <title>Beginning PHP, Apache, MySQL Web Development</title>
</head>
<body>
<?php
echo $error_output;
?>
</body>
</html>

Stage 2

error.php - Stage 2

<?
function e-mail_admin($error_no, $error_output, $full_date, $full_time,
$request_page)
{
     $to = "Administrator <admin@yourdomain.com>";

     $subject = "Apache Error Generation";

     $body = "<html>";
     $body .= "<head>";
     $body .= "<title></title>";
     $body .= "</head>";
     $body .= "<body>";
     $body .= "Error occurred on <b>" . $full_date . "</b> at <b>" . $full_time .
 "</b><br>";
     $body .= "Error received was a <b>" . $error_no . "</b> error.<br>";
     $body .= "The page that generated the error was: <b>" . $request_page .
 "</b><br>";
     $body .= "The generated error message was:" . $error_output;
     $body .= "</body>";
     $body .= "</html>";

     $headers = "MIME-Version: 1.0\r\n";
     $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

     $headers .= "From: Apache Error <host@yourdomain.com>\r\n";
     $headers .= "Cc: webmaster@yourdomain.com\r\n";

     mail($to, $subject, $body, $headers);
}

$date = getdate();
$full_date = $date['weekday'] . ", " . $date['month'] . " " . $date['mday'] . ", "
 . $date['year'];
$full_time = $date['hours'] . ":" . $date['minutes'] . ":" . $date['seconds'] . ":"
 . $date['year'];

$error_no = $_SERVER['QUERY_STRING'];
$request_page = $_SERVER['REQUEST_URI'];

switch ($error_no)
{
     case 400:
          $error_output = "<h1>\"Bad Request\" Error Page - (Error Code
 400)</h1>";
          $error_output .= "The browser has made a Bad Request<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
               the system administrator";
          $error_output .= " if you feel this to be in error";

          e-mail_admin($error_no, $error_output, $full_date, $full_time,
 $request_page);
     break;
     case 401:
          $error_output = "<h1>\"Authorization Required\" Error Page - (Error
 Code 401)</h1>";
          $error_output .= "You have supplied the wrong information to access a
 secure area<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
 the system administrator";
          $error_output .= " if you feel this to be in error";

          e-mail_admin($error_no, $error_output, $full_date, $full_time,
 $request_page);
     break;
     case 403:
          $error_output = "<h1>\"Forbidden Access\" Error Page - (Error Code
 403)</h1>";
          $error_output .= "You are denied access to this area<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
 the system administrator";
          $error_output .= " if you feel this to be in error";

          e-mail_admin($error_no, $error_output, $full_date, $full_time,
 $request_page);
     break;
     case 404:
          $error_output = "<h1>\"Page Not Found\" Error Page - (Error Code
 404)</h1>";
          $error_output .= "The page you are looking for cannot be found<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
 the system administrator";
          $error_output .= " if you feel this to be in error";

          e-mail_admin($error_no, $error_output, $full_date, $full_time,
 $request_page);
     break;
     case 500:
          $error_output = "<h1>\"Internal Server Error\" Error Page - (Error Code
 500)</h1>";
          $error_output .= "The server has encountered an internal error<br>";
          $error_output .= "<a href=\"mailto:sysadmin@localhost.com\">Contact</a>
 the system administrator";
          $error_output .= " if you feel this to be in error";

          e-mail_admin($error_no, $error_output, $full_date, $full_time,
 $request_page);
     break;
     default:
          $error_output = "<h1>Error Page</h1>";
          $error_output .= "This is the custom error Page<br>";
          $error_output .= "You should be <a href=\"index.php\">here</a>";
}
?>
<html>
<head>
     <title>Beginning PHP, Apache, MySQL Web Development</title>
</head>
<body>
<?
echo $error_output;
?>
</body>
</html>

Stage 3

error.php - Stage 3

<?php
//create your error handler function
function handler($error_type, $error_message, $error_file, $error_line)
{
     echo "<h1>Page Error</h1>";
     echo "Errors have occurred while executing this page. Contact the ";
     echo "<a href=\"mailto:admin@yourdomain.com\">administrator</a> to report
          errors<br><br>";
     echo "<b>Information Generated</b><br><br>";
     echo "<b>Error Type:</b> $error_type<br>";
     echo "<b>Error Message:</b> $error_message<br>";
     echo "<b>Error Filename:</b> $error_file<br>";
     echo "<b>Error Line:</b> $error_line";
}

//set the error handler to be used
set_error_handler("handler");

//set string with "Wrox" spelled wrong
$string_variable = "Worx books are great!";

//try to use str_replace to replace Worx with Wrox
//this will generate an E_WARNING
//because of wrong parameter count
str_replace("Worx", "Wrox");
?>

Stage 4

error.php - Stage 4

<?php
//create your error handler function
function handler($error_type, $error_message, $error_file, $error_line)
{
     switch($error_type)
     {
          //fatal error
          case E_ERROR:
               echo "<h1>Fatal Error</h1>";
               die("A fatal error has occured at line $error_line of file
                    $error_file.<br>
               Error message created was &quot;$error_message&quot;");
               break;
          //warnings
          case E_WARNING:
               echo "<h1>Warning</h1>";
               echo "A warning has occured at line $error_line of file
                    $error_file.<br>";
               echo " Error message created was &quot;$error_message&quot;";
          //notices
          case E_NOTICE:
               //don't show notice errors
               break;
     }
}

//set the error handler to be used
set_error_handler("handler");

//set string with "Wrox" spelled wrong
$string_variable = "Worx books are great!";

//try to use str_replace to replace Worx with Wrox
//this will generate an E_WARNING
//because of wrong parameter count
str_replace("Worx", "Wrox");
?>

Stage 5

error.php - Stage 5

<?php
//create your error handler function
function handler($error_type, $error_message, $error_file, $error_line)
{
     global $_SERVER['HTTP_HOST'], $_SERVER['HTTP_USER_AGENT'], $_SERVER
['REMOTE_ADDR'], $_SERVER['REQUEST_URI'];

     switch($error_type)
     {
          //fatal error
          case E_ERROR:
               $to = "Administrator <admin@yourdomain.com>";

               $subject = "Custom Error Handling";

               $body = "<html>";
               $body .= "<head>";
               $body .= "<title></title>";
               $body .= "</head>";
               $body .= "<body>";
               $body .= "<h1>Fatal Error</h1>";
               $body .= "Error received was a <b>" . $error_type . "</b>
error.<br>";
               $body .= "The page that generated the error was: <b>" .
$error_file . "</b>";
               $body .= " and was generated on line: <b>" . $error_line .
"</b><br>";
               $body .= "The generated error message was:" . $error_message;
               $body .= "</body>";
               $body .= "</html>";

               $headers = "MIME-Version: 1.0\r\n";
               $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

               $headers .= "From: Apache Error <host@yourdomain.com>\r\n";
               $headers .= "Cc: webmaster@yourdomain.com\r\n";

               mail($to, $subject, $message, $headers);
               die(); //kill the script
               break;
          //warnings
          case E_WARNING:

               $to = "Administrator <admin@yourdomain.com>";

               $subject = "Custom Error Handling";

               $body = "<html>";
               $body .= "<head>";
               $body .= "<title></title>";
               $body .= "</head>";
               $body .= "<body>";
               $body .= "<h1>Warning</h1>";
               $body .= "Error received was a <b>" . $error_type . "</b>
                     error.<br>";
               $body .= "The page that generated the error was: <b>" .
                    $error_file . "</b>";
               $body .= " and was generated on line: <b>" . $error_line .
                    "</b><br>";
               $body .= "The generated error message was:" . $error_message;
               $body .= "</body>";
               $body .= "</html>";

               $headers = "MIME-Version: 1.0\r\n";
               $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

               $headers .= "From: Apache Error <host@yourdomain.com>\r\n";
               $headers .= "Cc: webmaster@yourdomain.com\r\n";

               mail($to, $subject, $message, $headers);
               break;
               //script will continue

          //notices
          case E_NOTICE:
               //don't show notice errors
               break;
     }
}
/*
set error handling to 0
we will handle all error reporting
only notifying admin on warnings and fatal errors
don't bother with notices as they are trivial errors
really only meant for debugging
*/
error_reporting(0);

//set the error handler to be used
set_error_handler("handler");

/*
Create the rest of your page here.
We will not be displaying any errors
We will be e-mailing the admin an error message
Keep in mind that fatal errors will still halt the
execution, but they will still notify the admin
*/
?>