robcrozier Posted May 15, 2009 Share Posted May 15, 2009 Hi everyone. What i'm trying to do is stop my php file from executing after a certain amount of time (say 30 seconds). This is because the file will take a long time to fully execute. However, i want it to actually print something on the screen after it stops (i.e the information it has processed thus far). I do not want it to display a fatal error just like it would if i changed the php max execution time setting. Any ideas? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/ Share on other sites More sharing options...
nadeemshafi9 Posted May 15, 2009 Share Posted May 15, 2009 you could start a process wich chekcs the time and start another process wich does your script, in the process that does your script you can break out of that process and jump into the timmer process which checks the time if the time is ok that process breaks out and jumps back into your script process wich continues from where it jumped out then furtehr down the script yoiu can jump back out into the timer process and check the time again and if the time is too much you can return or pipe throgh a flag from the timer process so that when it jumps back into the script process it has a flag which tells the script process to die. http://uk3.php.net/manual/en/function.pcntl-fork.php Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-834863 Share on other sites More sharing options...
JonnoTheDev Posted May 15, 2009 Share Posted May 15, 2009 Output buffering http://uk3.php.net/manual/en/function.flush.php Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-834868 Share on other sites More sharing options...
Mark Baker Posted May 15, 2009 Share Posted May 15, 2009 Or even output buffering with a timeout handler: register_shutdown_function("say_goodbye"); function say_goodbye() { if (connection_aborted()) { // user has aborted the request, so perhaps redirect back to previous page } elseif (connection_status() == CONNECTION_TIMEOUT) { // PHP has hit timeout. Flush the output buffer } else { // Normal termination, so do nothing more } } Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-834901 Share on other sites More sharing options...
nadeemshafi9 Posted May 15, 2009 Share Posted May 15, 2009 you could start a process wich chekcs the time and start another process wich does your script, in the process that does your script you can break out of that process and jump into the timmer process which checks the time if the time is ok that process breaks out and jumps back into your script process wich continues from where it jumped out then furtehr down the script yoiu can jump back out into the timer process and check the time again and if the time is too much you can return or pipe throgh a flag from the timer process so that when it jumps back into the script process it has a flag which tells the script process to die. http://uk3.php.net/manual/en/function.pcntl-fork.php jajaja im dum you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-834927 Share on other sites More sharing options...
Mark Baker Posted May 16, 2009 Share Posted May 16, 2009 you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data Why bother? PHP will cease executing at it's predefined timeout anyway. Why not let PHP terminate as normal, but trap the timeout error and replace it with your own output. Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-835191 Share on other sites More sharing options...
nadeemshafi9 Posted May 17, 2009 Share Posted May 17, 2009 you can just do a function wich checks the time and if teh time is ove rteh limit then just die and echo data Why bother? PHP will cease executing at it's predefined timeout anyway. Why not let PHP terminate as normal, but trap the timeout error and replace it with your own output. will that be using teh ini set function becaus eyou dont whant to set it for the whole server ?? Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-835635 Share on other sites More sharing options...
Mark Baker Posted May 17, 2009 Share Posted May 17, 2009 will that be using teh ini set function becaus eyou dont whant to set it for the whole server ?? The php.ini setting is for the whole server, though it can be overridden by the web server timeout setting in httpd.conf (for apache), but you can over-ride it within your individual scripts using set_time_limit() or ini_set(). However it is set, when PHP reaches that time limit the script will terminate with a timeout. register_shutdown_function() allows you to define a block of code that will be executed whenever and however the script terminates. That can be a timeout, a user abort, a normal termination (through exit() or die() or simply the end of the script). You can then determine how the termination has been triggered, as shown in my example, and run a small block of code before ending execution. I use it to trap for timeouts in Ajax calls and return a status code to the browser, which can then take appropriate action (display a special message to the user offering them alternative functions such as re-submitting their request to run as a background task rather than interactively). Quote Link to comment https://forums.phpfreaks.com/topic/158288-stop-php-execution-and-print-results-not-fatal-error/#findComment-835732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.