Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm back again with a new problem.

 

My custom error handler works fine if there's only one error on the page, but if there's multiple errors, the first error is printed and the second error breaks the html (prints below my footer). I see what the logic error is... I'm printing my header, navigation and footer by using require_once. When there's two errors, the header, navigation and footer don't get printed but the body portion with the 2nd error does. How can I get all errors printed within the body while using my header, navigation and footer? Make the error an array?

 

function handler($error_type, $error_message, $error_file, $error_line, $error_variables){
 $message = "<p>An error of type '$error_type' has occurred in script '$error_file' on line $error_line:<br>\n";
 $message .= "$error_message<br>\n";
 $message .= "Date/Time: " . date('n-j-Y H:i:s') . "</p>\n";
 $message .= "<pre>" . print_r($error_variables, 1) . "</pre>\n";
 $messagewrapped = wordwrap($message, 75);
 $headers = "From: user@site.com";

if (!LIVE){
 require_once(HEADER);
 require_once(TOPNAV);
 echo "<div id=\"mainfull\">\n";
 echo "<h1>Error</h1>\n";
 echo $messagewrapped;
 echo "</div>\n";
 require_once(FOOTER);
} else {
 mail(EMAIL, 'Site Error!', strip_tags($messagewrapped), $headers);
 require_once(HEADER);
 require_once(TOPNAV);
 echo "<div id=\"mainfull\">\n";
 echo "<h1>Error</h1>\n";
 $notlive = "<p>A system error has occurred. Sorry for the problem.</p>\n";
 $notlive .= "<p><a href=\"index.php\">Return Home</a></p>\n</div>\n<br>\n\n";
 echo $notlive;
 require_once(FOOTER);
}
}
set_error_handler('handler');

 

I'm also having trouble catching fatal errors, it goes back to PHP's error handling, which doesn't let me use a switch statement to look for (E_ERROR). I did some reading and it seems those errors can't be caught, only user errors.

Link to comment
Share on other sites

Really the error handler function used in the param of set_error_hander() should be only defining the way to print the error whether site is live or not. I think the solution here is some code separation, it would be better to take out the HTML Header, Footer, and Nav stuff etc and have this printed by the main scripts. Just have the error handler only dealing with the printing or mailing of the error that's it.

 

Make a separate PHP or HTML script for your HEADER and FOOTER. You can also include the NAV in the HEADER script.

 

I would try to make these adjustments first then we can take a look at further errors, this is my advise.

  • Upvote 1
Link to comment
Share on other sites

Hi Lou,

 

What Edward is saying is correct, the reason the 2nd, 3rd, 4th etc, errors are appearing below the footer, is because the 1st error is showing the header then the error then the footer.

 

The 2nd error is then display just the error below the footer and not showing the header and footer again because you are using require_once().

 

The solution: remove all includes/requires from the error handler and let the core of your site handle these includes. Am I correct in assuming that you're worried that if there is an error before the header has been loaded you will get an ugly looking error message with no styling?

 

You shouldn't worry about this to much as any system files you modify should be heavily tested before publish to a production environment. However if it does really bug you, you could store the errors in an array and then once the header has been loaded loop over them and print them to screen. Do this you will need to check the header hasn't already been loaded because if it has you would need to show the errors instantly.

 

Regards

  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...