Quantcast
Channel: Dallas Clark - Web Developer, Facebook Developer, iPhone Developer, iPad Developer » script
Viewing all articles
Browse latest Browse all 12

PHP Error Control

$
0
0

I use this script quite frequently now on PHP projects for simple error reports straight to my email account. Your clients are well pleased when you have the bug/error fixed before they report it to you.

All you need to do is copy the following script and paste it into a file and include it into your project.

ie:

  1.  
  2. < ?php
  3. include('error_control.php');
  4. ?>
  5.  

The page error_control.php contains the script below

  1.  
  2. < ?php
  3.  
  4. $debug = true;
  5. define("EmailNewLine", "\r\n");
  6. set_error_handler('error_handler');
  7.  
  8. function error_handler($errno, $errstr, $errfile, $errline) {
  9. global $debug;
  10. // echo a message to the end user
  11. echo '<span class="error_message">There has been a major error, now that we know about this error we will fix it!';
  12.  
  13. $message = 'Date: '. date("d") .'/'. date("m") .'/'. date("Y") .' '. date("h") .':'. date("i") .':'. date("s") .' '. date("A") .'<br />';
  14. $message .= 'File: '. __FILE__ .'<br />';
  15. $message .= 'Line: '. __LINE__ .'<br />';
  16. $message .= '<br />';
  17.  
  18. switch ($errno) {
  19. case E_USER_ERROR:
  20. if ($errstr == "(SQL)") {
  21. // handling an sql error
  22. $message .= "<b>SQL Error</b> [$errno] " . SQLMESSAGE . "<br />";
  23. $message .= "Query : " . SQLQUERY . "<br />";
  24. $message .= "On line " . SQLERRORLINE . " in file " . SQLERRORFILE . " ";
  25. $message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />";
  26. $message .= "Aborting...<br />";
  27. } else {
  28. $message .= "<b>My ERROR</b> [$errno] $errstr<br />";
  29. $message .= " Fatal error on line $errline in file $errfile";
  30. $message .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />";
  31. $message .= "Aborting...<br />";
  32. }
  33. exit(1);
  34. break;
  35.  
  36. case E_USER_WARNING:
  37. $message .= "<b>My WARNING</b> [$errno] $errstr<br />";
  38. break;
  39.  
  40. case E_USER_NOTICE:
  41. $message .= "<b>My NOTICE</b> [$errno] $errstr<br />";
  42. break;
  43.  
  44. default:
  45. $message .= "Unknown error type: [$errno] $errstr<br />";
  46. break;
  47. }
  48.  
  49. $to = 'name@domain-name.com';
  50. $subject = 'WEBSITE ERROR: Name of project/web-site';
  51. $headers = "Date: " . date("r", time()) . EmailNewLine .
  52. "From: website_error@smg.com.au" . EmailNewLine .
  53. "Content-type: text/html; charset=iso-8859-1 ";
  54. mail($to, $subject, $message, $headers);
  55.  
  56. // if $debug is equal to true then show the full error on the website, when you
  57. // make the website live make sure you set $debug equal to false
  58. if($debug == true) {
  59. echo '<br /><span class="error_message">'. $message .'</span>';
  60. }
  61.  
  62. return true;
  63. }
  64.  
  65. ?>
  66.  

Viewing all articles
Browse latest Browse all 12

Trending Articles