Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Error Handling In PHP (part 2)
Use PHP's error API to trigger your own errors and log error messages to a file, database or email address.

| Raising Hell |

Thus far, we've been dealing with errors which are automatically generated by PHP when it encounters a problem. However, in addition to these built-in errors, PHP also allows you to raise errors of your own.

This is accomplished via a little function named trigger_error(), which allows you to raise any of the three errors in the E_USER family. These errors can then be caught and handled either by PHP's built-in error handler, or by a custom handler.

Here's a simple example which demonstrates how this works:


<?php

// function to validate password integrity
// generates an E_USER_WARNING if password fails a test
function validatePassword($pass)
{
   // empty string
   if(trim($pass) == "")
   {
      trigger_error("Empty password", E_USER_WARNING);
   }

   // too short
   if(strlen($pass) < 5)
   {
      trigger_error("Password too short", E_USER_WARNING);
   }

   // only numeric
   if(is_numeric($pass))
   {
      trigger_error("Password cannot contain only numbers", E_USER_WARNING);
   }
}

echo "<br>-- validating empty string -- <br>";

validatePassword("");

echo "<br>-- validating 12345 -- <br>";

validatePassword(12345);

echo "<br>-- validating Gh3 --<br>";

validatePassword("Gh3");

?>


Here's what the output looks like:


-- validating empty string --

Warning: Empty password in /usr/local/apache/htdocs/x2.php on line 10

Warning: Password too short in /usr/local/apache/htdocs/x2.php on line 16

-- validating 12345 --

Warning: Password cannot contain only numbers in /usr/local/apache/htdocs/x2.php on line 22

-- validating Gh3 --

Warning: Password too short in /usr/local/apache/htdocs/x2.php on line 16


In this case, every time the argument to validatePassword() fails one of the tests within the function, an E_USER_WARNING error will be raised; this error will be caught by PHP's built-in handler and handled in the same way as "regular" warnings - it will be displayed to the user, but script processing will not be halted.

It's also possible to raise fatal errors in this fashion. Consider the next example, which updates the validatePassword() function to raise a fatal error only if the password string is empty.


<?php

// function to validate password integrity
// generates an E_USER_WARNING if password fails a test
function validatePassword($pass)
{
   // empty string
   // trigger a fatal error
   if(trim($pass) == "")
   {
      trigger_error("Empty password", E_USER_ERROR);
   }

   // too short
   if(strlen($pass) < 5)
   {
      trigger_error("Password too short", E_USER_WARNING);
   }

   // only numeric
   if(is_numeric($pass))
   {
      trigger_error("Password cannot contain only numbers", E_USER_WARNING);
   }
}

echo "<br>-- validating 12345 -- <br>";

validatePassword(12345);

echo "<br>-- validating empty string -- <br>";

validatePassword("     ");

echo "<br>-- validating Gh3 --<br>";

validatePassword("Gh3");

?>


In this case, when the second password is evaluated, a fatal error will be raised, PHP's built-in handler will catch it, note that it is a fatal error and terminate script execution immediately. Here's what it looks like:


-- validating 12345 --

Warning: Password cannot contain only numbers in /usr/local/apache/htdocs/x2.php on line 23

-- validating empty string --

Fatal error: Empty password in /usr/local/apache/htdocs/x2.php on line 11


Note that the script never reaches the third call to validatePassword().

User-triggered errors can also be caught by a custom error handler, in much the same way as built-in errors. Let's see how, with a variant of the example on the previous page:


<?php

// function to validate password integrity
// generates an E_USER_WARNING if password fails a test
function validatePassword($pass)
{
   // empty string
   // trigger a fatal error
   if(trim($pass) == "")
   {
      trigger_error("Empty password", E_USER_ERROR);
   }

   // too short
   if(strlen($pass) < 5)
   {
      trigger_error("Password too short", E_USER_WARNING);
   }

   // only numeric
   if(is_numeric($pass))
   {
      trigger_error("Password cannot contain only numbers", E_USER_WARNING);
   }
}

// custom error handler
function eh($type, $msg, $file, $line, $context)
{
   switch($type)
   {
      // user-triggered fatal error
      case E_USER_ERROR:
         echo "A fatal error occurred at line $line of file $file. The error message was <b>$msg</b> <br>";
         echo "<font color=red><i>Script terminated</i></font>";
         die();
         break;

      // user-triggered warning
      case E_USER_WARNING:
         echo "A non-trivial, non-fatal error occurred at line $line of file $file. The error message was <b>$msg</b> <br>";
         break;

      // user-triggered notice
      case E_USER_NOTICE:
         echo "A trivial, non-fatal error occurred at line $line of file $file. The error message was <b>$msg</b> <br>";
         break;
   }
}

// define custom handler
set_error_handler("eh");

echo "<br>-- validating 12345 -- <br>";

validatePassword(12345);

echo "<br>-- validating empty string -- <br>";

validatePassword("     ");

echo "<br>-- validating Gh3 --<br>";

validatePassword("Gh3");

?>


In this case, the user-generated errors are routed to the custom error handler, which prints a user-defined error message and - if the error was defined as fatal - terminates script execution.

Here's what it looks like:


-- validating 12345 --
A non-trivial, non-fatal error occurred at line 23 of file /usr/local/apache/htdocs/x2.php. The error message was Password cannot contain only numbers

-- validating empty string --
A fatal error occurred at line 11 of file /usr/local/apache/htdocs/x2.php. The error message was Empty password
Script terminated


And here's a picture:

Output image

Note that it is the responsibility of the custom handler to die() in the event of user-generated fatal errors - PHP will not do this automatically.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2017 Melonfire. All rights reserved
Terms and Conditions | Feedback