Php Fatal Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of php register_shutdown_function this site About Us Learn more about Stack Overflow the company Business Learn php catch fatal error and continue more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Php Set_error_handler
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How
Register_shutdown_function Fatal Error
do I catch a PHP Fatal Error up vote 414 down vote favorite 181 I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal (E\_ERROR) errors, such as calling a function that doesn't exist. Is there another way to catch these errors? I am trying to call mail() for all errors and am running PHP 5.2.3. php fatal-error what is fatal error in php share|improve this question edited Dec 22 '13 at 19:26 aksu 3,94451434 asked Nov 10 '08 at 6:42 too much php 41.9k26107123 add a comment| 17 Answers 17 active oldest votes up vote 506 down vote Log fatal errors using register_shutdown_function, which requires PHP 5.2+: register_shutdown_function( "fatal_handler" ); function fatal_handler() { $errfile = "unknown file"; $errstr = "shutdown"; $errno = E_CORE_ERROR; $errline = 0; $error = error_get_last(); if( $error !== NULL) { $errno = $error["type"]; $errfile = $error["file"]; $errline = $error["line"]; $errstr = $error["message"]; error_mail(format_error( $errno, $errstr, $errfile, $errline)); } } You will have to define the error_mail and format_error functions. For example: function format_error( $errno, $errstr, $errfile, $errline ) { $trace = print_r( debug_backtrace( false ), true ); $content = "
Item | Description |
---|---|
Error | $errstr |
Errno | $errno |
File | $errfile |
Line | $errline |
Trace | $trace |
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported
Fatal Error In Php Means
Protocols and Wrappers Security Introduction General considerations Installed as CGI php error_get_last binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using php 7 catch fatal error Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions http://php.net/manual/en/function.set-error-handler.php Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box set_exception_handler » « restore_exception_handler PHP Manual Function Reference Affecting PHP's Behaviour Error Handling Error Handling Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug set_error_handler (PHP 4 >= 4.0.1, PHP 5, PHP 7)set_error_handler — Sets a user-defined error handler function Description mixed set_error_
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes http://php.net/manual/en/class.errorexception.php Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security http://www.xpertdeveloper.com/2013/01/catch-fatal-errors-in-php/ Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with fatal error PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit catch fatal error Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box ErrorException::__construct » « Exception::__clone PHP Manual Language Reference Predefined Exceptions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Tur
can’t cache FATAL Error because those are FATAL. This will be useful when you want to show specific well formatted error page rather than PHP Error, when your program ends unexpectedly due to some fatal errors. We will use register_shutdown_function function to catch the fatal errors and format as we need. Keep in mind that register_shutdown_function will gets executed after your script execution ends either it is successful or ends with any error. So before performing any action in this function we need to check if really any fatal occurs or not. If occurs then we will show error page else it will continue to run as it is. So to get the details of the last error we will use error_get_last() function. Code function catch_fatal_error() { // Getting Last Error $last_error = error_get_last(); // Check if Last error is of type FATAL if(isset($last_error['type']) && $last_error['type']==E_ERROR) { // Fatal Error Occurs // Do whatever you want for FATAL Errors } } register_shutdown_function('catch_fatal_error');function catch_fatal_error() { // Getting Last Error $last_error = error_get_last(); // Check if Last error is of type FATAL if(isset($last_error['type']) && $last_error['type']==E_ERROR) { // Fatal Error Occurs // Do whatever you want for FATAL Errors } } register_shutdown_function('catch_fatal_error'); Share on Facebook Share on Twitter Related Posts PHP Error HandlingPHP Error ReportingEnable PHP Error logging using htaccessType Hinting in PHPAbstract in PHP Written by Avinash Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter. View all posts by: Avinash MM Actually Good point is highlighted that register_shutdown_function will be executed even i