PHP Custom Error Handling and Emailing


/ Published in: PHP
Save to your folder(s)

Refer here for a full overview: http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // Our custom error handler
  4. function nettuts_error_handler($number, $message, $file, $line, $vars)
  5.  
  6. {
  7. $email = "
  8. <p>An error ($number) occurred on line
  9. <strong>$line</strong> and in the <strong>file: $file.</strong>
  10. <p> $message </p>";
  11.  
  12. $email .= "<pre>" . print_r($vars, 1) . "</pre>";
  13.  
  14. $headers = 'Content-type: text/html; charset=iso-8859-1' . "
  15. ";
  16.  
  17. // Email the error to someone...
  18. error_log($email, 1, '[email protected]', $headers);
  19.  
  20. // Make sure that you decide how to respond to errors (on the user's side)
  21. // Either echo an error message, or kill the entire project. Up to you...
  22. // The code below ensures that we only "die" if the error was more than
  23. // just a NOTICE.
  24. if ( ($number !== E_NOTICE) && ($number < 2048) ) {
  25. die('There was an error. Please try again later.');
  26. }
  27. }
  28.  
  29. // We should use our custom function to handle errors.
  30. set_error_handler('nettuts_error_handler');
  31.  
  32. // Trigger an error... (var doesn't exist)
  33. echo $somevarthatdoesnotexist;

URL: http://net.tutsplus.com/tutorials/php/quick-tip-email-error-logs-to-yourself-with-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.