{"id":170,"date":"2015-12-05T16:27:00","date_gmt":"2015-12-05T14:27:00","guid":{"rendered":"https:\/\/celilsemi.erkiner.com\/blog\/?p=170"},"modified":"2020-05-25T17:02:26","modified_gmt":"2020-05-25T15:02:26","slug":"php-error-reporting-via-emails","status":"publish","type":"post","link":"https:\/\/celilsemi.erkiner.com\/blog\/php-error-reporting-via-emails\/","title":{"rendered":"PHP Error reporting via Emails"},"content":{"rendered":"\n<p>This tutorial provides a solution to receive email alerts when an uncontrolled error occurs.<\/p>\n\n\n\n<p>Imagine that you have created a PHP application, hosted on an Apache Server and it is working smoothly. A few days later, a user complains about an error that is not known to you completely. Such unprocessed errors are very common in PHP applications. Therefore, to find these issues, we will program the PHP project to send email notifications when an error occurs.<\/p>\n\n\n\n<p>For this to happen we need to detect and react when there is a fatal error that causes execution to stop unexpectedly. There is a PHP hook we can utilize for this purpose; register_shutdown_function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is register_shutdown_function?<\/h2>\n\n\n\n<p>register_shutdown_function is used to register a function for execution on shutdown.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">register_shutdown_function ( callable $callback [, mixed $... ] ) : void<\/pre><\/div>\n\n\n\n<p>Registers a\u00a0callback\u00a0to be executed after script execution finishes or\u00a0exit()\u00a0is called.<\/p>\n\n\n\n<p>Multiple calls to\u00a0<strong>register_shutdown_function()<\/strong>\u00a0can be made, and each will be called in the same order as they were registered. If you call\u00a0exit()\u00a0within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.<\/p>\n\n\n\n<p>When this function is called we can simply determine if and what error caused the execution to stop. This registration and check must be accessible by all the PHP files.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">register_shutdown_function('mail_on_error');\n\nfunction mail_on_error() {\n  $error = error_get_last(); \n  if ($error['type'] == 1) {\n    $emailAddress = 'example@example.com';\n    $emailSubject = 'Error on my PHP Application';\n    $emailMessage = '&lt;h2&gt;Error Reporting on :- &lt;\/h2&gt;[' . date(&quot;Y-m-d h:i:s&quot;, time()) . ']';\n    $emailMessage .= &quot;&lt;h2&gt;Error Number :- &lt;\/h2&gt;&quot;.print_r($error['type'], true).'';\n    $emailMessage .= &quot;&lt;h2&gt;Error String :- &lt;\/h2&gt;&quot;.print_r($error['message'], true).'';\n    $emailMessage .= &quot;&lt;h2&gt;Error File :- &lt;\/h2&gt;&quot;.print_r($error['file'], true).'';\n    $emailMessage .= &quot;&lt;h2&gt;Error Line :- &lt;\/h2&gt;&quot;.print_r($error['line'], true).'';\n    $headers = &quot;MIME-Version: 1.0&quot; . &quot;rn&quot;;\n    $headers .= &quot;Content-type:text\/html;charset=UTF-8&quot; . &quot;rn&quot;;\n    mail($emailAddress, $emailSubject, $emailMessage, $headers);\n  }\n}<\/pre><\/div>\n\n\n\n<p>This PHP snippet will prepare an informative email and sent it to &#8216;example@example.com&#8217; when an important error occurs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial provides a solution to receive email alerts when an uncontrolled error occurs. Imagine that you have created a PHP application, hosted on an Apache Server and it is working smoothly. A few days later, a user complains about an error that is not known to you completely. Such unprocessed errors are very common [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[23,25,16],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-back-end","tag-apache","tag-php","tag-server"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":1,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/posts\/170\/revisions\/171"}],"wp:attachment":[{"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/celilsemi.erkiner.com\/blog\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}