H
H
himiza2020-10-02 14:45:16
PHP
himiza, 2020-10-02 14:45:16

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

4 answer(s)
N
neol, 2020-10-02
@himiza

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

For apache, you can look towards TimeOut, but I'm not sure if this will work exactly as it should (haven't used apache for a long time).

A
Anton Shamanov, 2020-10-02
@SilenceOfWinter

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

tests to the studio)
register_tick_function will help solve this abstract problem

S
Stalker_RED, 2020-10-02
@Stalker_RED

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);

© so

C
Crabster, 2020-10-03
@Crabster

set_time_limit(30);
And the script on the drums what's going on inside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question