•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 375,193 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,178 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 481 | Replies: 1
![]() |
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
We struggle to come up with a decent error handling function. I thought that I use a global variable $live to easily make a switch from testing to live. I will keep the function in an include file.
I kind of got there to print a "user-friendly" message when the site is live, but it messes up the whole page layout. Don't how to print the error message in the content area. How would I adjust my error handler and what would I add to "<?php require_once $content; ?>"?
My error handler:
<?php
/*-- start output buffering--*/
ob_start();
/*-- live site --*/
$live=TRUE;
/*-- error handling function giving back error number, the actual message, the error file, the error line and the variables (what are variable in this context?) --*/
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
/*-- make $live global --*/
global $live;
/*-- set error message for testing together --*/
$message="Error in file '$e_file' on line $e_line: $e_message\n";
$message.=print_r($e_vars,1);
/*-- error message for live site should not give any detail about problem, just an apology --*/
if($live){
$user="We apologise for the problems on this page...";
/*-- if there are error messages print the standard user message --*/
if ($message==0){echo $user;}else{
/*-- otherwise say 'hi there' --*/
echo ('');}
/*-- do it only once as user should not see the apology more than once --*/
exit();
} else {
/*-- if site is in testing print the acual error messages --*/
echo '<div class="error">'.$message.'</div><br/>';
}
}
ob_end_flush();
/*-- as everything is defined now, set the error handler, so it's ready to be used --*/
set_error_handler('my_error_handler');
?>
Also, in the bit "if ($message==0)" I would have thought that comparing $message to 1 would have printed the user-friendly error message (as I thought 1 means that there is one or more error message).
Any help appreciated.
I kind of got there to print a "user-friendly" message when the site is live, but it messes up the whole page layout. Don't how to print the error message in the content area. How would I adjust my error handler and what would I add to "<?php require_once $content; ?>"?
My error handler:
<?php
/*-- start output buffering--*/
ob_start();
/*-- live site --*/
$live=TRUE;
/*-- error handling function giving back error number, the actual message, the error file, the error line and the variables (what are variable in this context?) --*/
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
/*-- make $live global --*/
global $live;
/*-- set error message for testing together --*/
$message="Error in file '$e_file' on line $e_line: $e_message\n";
$message.=print_r($e_vars,1);
/*-- error message for live site should not give any detail about problem, just an apology --*/
if($live){
$user="We apologise for the problems on this page...";
/*-- if there are error messages print the standard user message --*/
if ($message==0){echo $user;}else{
/*-- otherwise say 'hi there' --*/
echo ('');}
/*-- do it only once as user should not see the apology more than once --*/
exit();
} else {
/*-- if site is in testing print the acual error messages --*/
echo '<div class="error">'.$message.'</div><br/>';
}
}
ob_end_flush();
/*-- as everything is defined now, set the error handler, so it's ready to be used --*/
set_error_handler('my_error_handler');
?>
Also, in the bit "if ($message==0)" I would have thought that comparing $message to 1 would have printed the user-friendly error message (as I thought 1 means that there is one or more error message).
Any help appreciated.
Buy Discount Cartridge at LowerPriceUSA.com credit cards for bad credit history
•
•
Join Date: Dec 2007
Posts: 75
Reputation:
Rep Power: 1
Solved Threads: 10
>>Don't how to print the error message in the content area
You need to trigger the error only while printing the content. For example the code below will NOT print it within the content because you are not triggering the error within the content:
You can work around it by simply setting a variable to TRUE if trigger_error should be called, but delay the call until the content.
You would also need to put:
global $triggerIt=FALSE;
in the same file where you are defining your errorHandler function. IF you are using some sort of templating system, then the
if( TRUE==$triggerIt){...}else{...}
part would be placed in the template. On my example, the getContent() should be a function defined on a per-page basis that returns the content for the page in question as a string. However, if you are using some third-party templating system, you would need to find out how they are emitting the content.
Short answer to your question, I believe you are triggering the error_handler outside of the content. In case you haven't seen it, there is a sample error handler at:
http://us2.php.net/manual/en/functio...or-handler.php
You need to trigger the error only while printing the content. For example the code below will NOT print it within the content because you are not triggering the error within the content:
php Syntax (Toggle Plain Text)
<?php $x = (int)$_POST['INPUT']; if( $x==3) trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING); ?> <html> <body> <?php getContent(); ?> </body> </html>
You can work around it by simply setting a variable to TRUE if trigger_error should be called, but delay the call until the content.
php Syntax (Toggle Plain Text)
<?php global $triggerIt=FALSE; $x = (int)$_POST['INPUT']; if( $x==3) ?> <html> <body> <?php if( TRUE==$triggerIt) { trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING); } else { ?> getContent(); <?php } ?> </body> </html>
You would also need to put:
global $triggerIt=FALSE;
in the same file where you are defining your errorHandler function. IF you are using some sort of templating system, then the
if( TRUE==$triggerIt){...}else{...}
part would be placed in the template. On my example, the getContent() should be a function defined on a per-page basis that returns the content for the page in question as a string. However, if you are using some third-party templating system, you would need to find out how they are emitting the content.
Short answer to your question, I believe you are triggering the error_handler outside of the content. In case you haven't seen it, there is a sample error handler at:
http://us2.php.net/manual/en/functio...or-handler.php
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- error handling in VB6 + + ODBC + MySQL...(Help!!!!) (Visual Basic 4 / 5 / 6)
- Error Throwing In Calculator Code When Divided By '0' (C#)
- Error Handling in CFSCRIPT (ColdFusion)
- Handling errors in php! ??? (PHP)
- Compilation Error Handles cmdLogin.ServerClick (ASP.NET)
- Another mysql_num_rows(): error (PHP)
- Keyboard Error Handling (C++)
Other Threads in the PHP Forum
- Previous Thread: Switch Statement output error
- Next Thread: How put "text" directly next to a variable in a URL?


Linear Mode