I have script that takes long time to execute. In a result server responds in 503 error. How can I set longer execution time?
In my PHP script I set:
set_time_limit(0);
ignore_user_abort(true);
This question is very similar to PHP Background Processes. So read that; In addition,
First you need to change the time limit in php.ini; you cannot change it in the script itself. Or in .htaccess using php_value
Second, you may be you want to modify how you interact with user more friendly so that the page uses an update; you can explore ajax; http-refresh; multipart pages for different approaches. If you are doing long calculations perhaps you can launch them in the background and update a database, retrieve the results later.
Despite what time-limit you set in the script, a web request might time out earlier because the client gives up, or because any proxy server in the middle gives up, or possibly if the apache server is configured to give up.
As the other answerer mentioned; the maximum_execution_time
variable might help, but it would be better if you didn't leave the visitor waiting with a long delay.
Consider using a pattern that shows feedback:
- User initiates action; eg POST request
- PHP Script returns immediately with a page containing javascript or an iframe.
- The javascript/inner frame calls the long-running script.
- Every few seconds the javascript updates the user on how the script is going, using a LONGPOLL-like mechanism.
- On completion or error, the javascript notifies and redirects to the next page.
Have a look at maximum_execution_time.
However to my knowledge this usually needs to be set in the php.ini , on a brief look at the manual it says this should be settable by scripts if not in safe mode.
You don't know the exact error? Without it, my guess is the memory limit, have you changed that too? Check your php.ini:
memory_limit = 128M
来源:https://stackoverflow.com/questions/8202002/long-computation-in-php-results-in-503-error