This is a simple way to handle your errors and warnings and provide users with a feedbackfor their actions.
The logic
Write an error handler and assign it to the php built-in error handling mechanism (set_error_handler(”yourhandler”)) where ever you want it to operate. In my case I put it together with the function definition in the file where I keep all my other defaults as such file gets included in all the files of my project, so I avoid to rewrite code for each other file.
The error handler will throw an exception for every error or warning encountered. So, whenever you wish your user to get a message (whether for error or success), put the critical code in a try/catch construct and in the catch body set two session variables, one “alerts” (String) containing the positive/negative messages and one called “input_errors” (Array) which contains the error parameters if any (e.g. if input from forms). Be sure you exit the script or the code that follows will be executed (usually you don’t want that especially if a nasty error had occurred).
In the html pages, retrieve the content of the session variables for disaplay (if the variable “input_errors” is empty, we assume that it is a success message) and then delete them from the session.
Implementation
in the file where you keep all your settings and therefore is contained in all the others (def_inc.php susually):
<?
……..
//ERROR HANDLING
//setting general error handler
function handleError($errno, $errstr,$error_file,$error_line,$errcontext)
{
$msg= “Sorry some error occurred. “;
$msg.= “<b>Error:</b> [$errno] $errstr – $error_file:$error_line”;
$msg.= “<br />”;
putsession(”alerts”,$msg);
putsession(”input_errors”, “some error”);
throw new Exception($msg);
}
//set error handler
set_error_handler(”handleError”);
………..
?>
In the template for html files which display the feedback messages to the user:
<HEAD>
……….
//handle alerts
$alerts=null;
//check for alerts or errors back from the processing code (putsession(’alerts’,'bla,bla…’),putsession(’errors’,parameter_errors()))
$input_rules["input_errors"][_structure]=_either_structure;
$input_rules["input_errors"][_sources]=_session;
$input_rules["input_errors"][_required]=false;
$input_rules["input_errors"][_fatal]=false;
$input_rules["alerts"][_structure]=_either_structure;
$input_rules["alerts"][_sources]=_session;
$input_rules["alerts"][_required]=false;
$input_rules["alerts"][_fatal]=false;
$parameters = validate_parameters($input_rules);
$errors = isset($parameters["input_errors"])?$parameters["input_errors"]:null;
$alerts = isset($parameters["alerts"])?$parameters["alerts"]:null;
delsession(”input_errors”); //reset input errors to null
delsession(”alerts”); //reset alerts to null
…….
</HEAD>
<BODY>
….
<? //alert and error system
if(isset($alerts)||isset($errors)){?>
<div id=”alerts”><?
if(isset($errors)){?>
<div class=”fail”><?=$alerts?></div>
<? }else{?>
<div class=”success”><?=$alerts?></div>
<? } ?>
</div>
<?
if(!empty($errors["access"]))return;
}//end alerts box
?>
…..
</BODY>