How To Set Custom Error Handler In PHP
Wednesday, April 27, 2011 9:01 PM
When nonindustrial a web application, nonachievement direction is very important. Standard nonachievement direction in PHP is very simple. Errors are dispatched to the browser contains an nonachievement message, enter name, how some lines of cipher to the environs of the error. Without a beatific nonachievement handling, of course, in the circumstance of an error, an nonachievement module materialize in the browser and viewed by all users. This could pose a danger to the security of the website. PHP provides functions for nonachievement trainer function. In addition to providing accepted nonachievement handling, PHP also provides the knowledge to create a duty assigned to handle errors that become is titled the Custom Error Handler. A Custom Error Handler info has a same this : php, nonachievement handler, website, script, how to error_funciton($error_level,$error_message, $error_file,$error_line,$error_context) <?php duty customError($errorLevel,$errorMsg) { @session_start();ÃÂ $_SESSION['error_msg'] = $errorMsg;ÃÂ header("Location: error.php");ÃÂ exit; } set_error_handler("customError");ÃÂ error_reporting(E_ALL);ÃÂ $connection = mysql_connect("localhost", "user", "password");ÃÂ mysql_select_db("test");ÃÂ $result = mysql_query("select * from table");ÃÂ while($row = mysql_fetch_array($result)) {ÃÂ reflexion $row['nama_field'];ÃÂ } ?> php, nonachievement handler, website, script, how to
Line 2-7 is an example of a Custom Error Handler, an nonachievement message saved into a session, and the individual condemned to a page “error.php” in the circumstance of an error. In line 8 utilised duty set_error_handler ($ handler); to specify a Custom Error Handler utilised in the circumstance of an error. Parameter string $ trainer live with a Custom Error Handler duty name. File “error.php” could look same this.php, nonachievement handler, website, script, how to
<? php session_start (); ?> <html> <head> <title> Error </ title> </ head> <body> <h1> Error </ h1> <p> Sorry we crapper not impact your request. </ p> <p> Error Message: <? php reflexion $ _SESSION ['error_msg'];?></ p> </ body> </ html>PHP has a ultimate nonachievement handler. Errors are dispatched to the browser contains an nonachievement message, enter name, how some lines of cipher to the environs of the error. PHP provides nonachievement direction capabilities better move than using the duty die () to ingest a bespoken nonachievement handler. With the duty error_reporting () crapper be filtered nonachievement nonachievement whatever you poverty to display. To not display the nonachievement duty crapper be utilised ini_set (‘display_errors’, ‘Off’); and duty ini_set (‘error_log’, ‘logs / error.log’); to save the nonachievement into the enter “logs / error.log”, using duty trigger_error () to create bespoken nonachievement messages, as substantially as duty error_log () to send emails in the circumstance of an error.
Refrence :
- http://www.php.net/manual/en/book.errorfunc.php
- http://www.w3schools.com/php/php_error.asp
Source
0 comments:
Post a Comment