Answer the question
In order to leave comments, you need to log in
How to really limit the maximum execution time of a PHP script?
Hello!
As you know, max_execution_time can limit the script execution time, however, if, for example, file_get_contents is called during execution, which will request a url that responds, for example, 100 seconds, then with max_execution_time 30 seconds, the script will still run for 100 seconds. This is actually stated in the PHP documentation.
Question: is it really possible to somehow limit the execution time in this case to 30 seconds, while not setting the file_get_contents context or setting the maximum time for sockets. That is, the unconditional termination of the script is of interest, regardless of what is in the code.
Thank you!
Answer the question
In order to leave comments, you need to log in
For php-fpm there is a request_terminate_timeout
The timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. A value of '0' means 'off'. Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
Default Value: 0
however, if during execution, for example, file_get_contents is called, which will request a url that responds, for example, 100 seconds, then with max_execution_time 30 seconds, the script will still be executed for 100 seconds
You can also set limits for file_get_contents
ini_set('default_socket_timeout', 30);
$ctx = stream_context_create(array('http'=>
array(
'timeout' => 1200, //1200 Seconds is 20 Minutes
)
));
echo file_get_contents('http://example.com/', false, $ctx);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question