Home arrow PHP arrow Page 6 - Error Handling In PHP (part 2)

Buffer Zone - PHP

The first part of this article demonstrated basic errorhandling in PHP, explaining the various error types and illustrating theprocess of building a custom error handler. But that's just the tip ofthe iceberg - this concluding part goes a step further, showing you totrigger your own errors, and log error messages to a file, database oremail address.

TABLE OF CONTENTS:
  1. Error Handling In PHP (part 2)
  2. Raising Hell
  3. Rolling Back
  4. Turning Up The Heat
  5. Of Form And Function
  6. Buffer Zone
  7. Back To Class
  8. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 11
April 09, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
You might remember, from the first part of this article, an example which demonstrated how a custom error handler could be used to write error messages to a file on a PHP-driven Web site. You might also remember that one of the drawbacks of that script was the fact that warnings would get printed while the page was being constructed.

It's possible to bypass this problem - and also simplify the error logging mechanism used in that example - via a series of judicious calls to PHP's output-buffering functions. Take a look at this revised script, which sets things up just right:

<?php // use an output buffer to store page contents ob_start(); ?> <html> <head><basefont face="Arial"></head> <body> <h2>News</h2> <?php // custom error handler function e($type, $msg, $file, $line) { // read some environment variables // these can be used to provide some additional debug information global $HTTP_HOST, $HTTP_USER_AGENT, $REMOTE_ADDR, $REQUEST_URI; // define the log file $errorLog = "error.log"; // construct the error string $errorString = "Date: " . date("d-m-Y H:i:s", mktime()) . "\n"; $errorString .= "Error type: $type\n"; $errorString .= "Error message: $msg\n"; $errorString .= "Script: $file($line)\n"; $errorString .= "Host: $HTTP_HOST\n"; $errorString .= "Client: $HTTP_USER_AGENT\n"; $errorString .= "Client IP: $REMOTE_ADDR\n"; $errorString .= "Request URI: $REQUEST_URI\n\n"; // log the error string to the specified log file error_log($errorString, 3, $errorLog); // discard current buffer contents // and turn off output buffering ob_end_clean(); // display error page echo "<html><head><basefont face=Arial></head><body>"; echo "<h1>Error!</h1>"; echo "We're sorry, but this page could not be displayed because of an internal error. The error has ben recorded and will be rectified as soon as possible. Our apologies for the inconvenience. <p> <a href=/>Click here to go back to the main menu.</a>"; echo "</body></html>"; // exit exit(); } // report warnings and fatal errors error_reporting(E_ERROR | E_WARNING); // define a custom handler set_error_handler("e"); // attempt a MySQL connection $connection = @mysql_connect("localhost", "john", "doe"); mysql_select_db("content"); // generate and execute query $query = "SELECT * FROM news ORDER BY timestamp DESC"; $result = mysql_query($query, $connection); // if resultset exists if (mysql_num_rows($result) > 0) { ?> <ul> <?php // iterate through query results // print data while($row = mysql_fetch_object($result)) { ?> <li><b><?=$row->slug?></b> <br> <font size=-1><i><?=$row->timestamp?></i></font> <p> <font size=-1><?php echo substr($row->content, 0, 150); ?>... <a href=story.php?id=<?=$row->id?>>Read more</a></font> <p> <?php } ?> </ul> <?php } else { echo "No stories available at this time"; } // no errors occured // print buffer contents ob_end_flush(); ?> </body> </html>
In this case, the first thing I've done is initialized the output buffer via a call to ob_start() - this ensures that all script output is placed in a buffer, rather than being displayed to the user. This output may be dumped to the standard output device at any time via a call to ob_end_flush().

Now, whenever an error occurs, my custom error handler, cleverly named e(), will first flush the output buffer, then send a custom error template to the browser and terminate script execution. So, even if there was a Web page being constructed on the fly when the error occurred, it will never see the light of day, as it will be discarded in favour of the custom error template. If, on the other hand, the script executes without any errors, the final call to ob_end_flush will output the fully-generated HTML page to the browser.

Note that, as before, fatal errors cannot be handled by the custom handler. The only way to avoid the output of fatal error messages is by telling PHP not to display them (take a look at the "display_errors" configuration directive in the PHP configuration file).

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: