{"id":5698,"date":"2019-11-05T14:54:30","date_gmt":"2019-11-05T19:54:30","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=5698"},"modified":"2019-11-05T14:54:30","modified_gmt":"2019-11-05T19:54:30","slug":"handling-php-execution-timeout","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=5698","title":{"rendered":"Handling PHP Execution Timeout"},"content":{"rendered":"<p>There&#8217;s no straight-forward way to handle execution timeout in PHP 5.x &#8212; it&#8217;s not like you can try\/except or something. The execution time-limit is exceeded, the program terminates. Which, thinking from the perspective of the person who maintains the server, is a Good Thing &#8230; bugger up the &#8216;except&#8217; component and now <em>that<\/em> becomes an infinite loop.<\/p>\n<p>But I&#8217;m looking to throw a &#8220;pretty&#8221; error to the end user and have them try again with a data set that will take less time to process. Turns out, you can use a <a href=\"https:\/\/www.php.net\/manual\/en\/function.register-shutdown-function.php\" target=\"_blank\" rel=\"noopener noreferrer\">shutdown function<\/a> to display something <em>other<\/em> than the generic PHP time limit exceeded page.<\/p>\n<pre>&lt;?php\r\n\r\nfunction runOnShutdown(){\r\n     $arrayError = error_get_last();\r\n\r\n     if( substr($arrayError['message'], 0, strlen(\"Maximum execution time of\")) === \"Maximum execution time of\"   ){\r\n          echo \"&lt;P&gt;Maximum execution time\";\r\n     }\r\n     elseif($arrayError){\r\n          print_r($arrayError);\r\n     }\r\n}\r\n\r\nfunction noOp($iInteger){\r\n     for($z = 0; $z &lt; $iInteger; $z++){\r\n          $a = $iInteger * $iInteger;\r\n     }\r\n     return $iInteger;\r\n}\r\n\r\nregister_shutdown_function('runOnShutdown');\r\nini_set('display_errors', '0');\r\nini_set('max_execution_time', 2);\r\n\r\n\/\/ for($i = 0; $i &lt; 10; $i++){\r\nfor($i = 0; $i &lt; 10000; $i++){\r\n     $j = noOp($i);\r\n     print \"&lt;P&gt;$j&lt;\/P&gt;\\n\";\r\n}\r\nprint \"&lt;P&gt;Done&lt;\/P&gt;\\n\";\r\n\r\n?&gt;<\/pre>\n<p>And the web output includes a customized message because the max execution time has been exceeded.<\/p>\n<p><a href=\"https:\/\/www.rushworth.us\/lisa\/?attachment_id=5699\" rel=\"attachment wp-att-5699\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5699\" src=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/11\/php-maxExecutionTime.png\" alt=\"\" width=\"316\" height=\"239\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/11\/php-maxExecutionTime.png 316w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2019\/11\/php-maxExecutionTime-300x227.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s no straight-forward way to handle execution timeout in PHP 5.x &#8212; it&#8217;s not like you can try\/except or something. The execution time-limit is exceeded, the program terminates. Which, thinking from the perspective of the person who maintains the server, is a Good Thing &#8230; bugger up the &#8216;except&#8217; component and now that becomes an &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[35],"class_list":["post-5698","post","type-post","status-publish","format-standard","hentry","category-coding","tag-php"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5698"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5698\/revisions"}],"predecessor-version":[{"id":5700,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/5698\/revisions\/5700"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}