I have a php script that will surely take very long time to process. Is it good to have it in set_time_limit(0); I have seen some times even when we set time limit to 0 we get 500 internal server error. What is best way to handle such long time consuming script in php?
-
1set_time_limit(0); is a better option , i have been using it to execute very large amt of data and its working fine... – Php Geek Mar 01 '13 at 04:49
-
Just keep in mind when you set_time_limit, the timer gets reset. so if you are iterating through an array, set some appropriate time in the foreach loop. this will prevent the process taking years but still gives enough time to execute the iteration. – AKS Mar 01 '13 at 05:28
3 Answers
If you try to execute a long running script from the browser, you have a chance of running into all sorts of timeouts, from php, proxies, web servers and browsers. You may not be able to control all of them.
Any long running script should be run from the command line. That way you take out all of the problems except for the php execution time limit which is easy to override.

- 19,153
- 2
- 48
- 54
If you have access to php.ini file then set max_execution_time in it like
max_execution_time = 300
or you can user .htaccess to handle it like
php_value max_execution_time 300

- 6,882
- 7
- 39
- 67
-
possible duplicate of : http://stackoverflow.com/questions/5451503/phps-set-time-limit-throws-a-500-error – alwaysLearn Mar 01 '13 at 05:01
Script which runs more than 360 seconds(I hope max_execution_time
) will throw an Internal server error automatically even set_time_limit
is applied.
You can have this in your page.
<?php
ini_set('max_input_time', time in seconds);
ini_set('max_execution_time', time in seconds);
?>
or in php.ini
set max_input_time
and max_execution_time
whatever you like.
or in .htaccess
php_value max_input_time 300
php_value max_execution_time 300

- 1,859
- 3
- 26
- 45