why timeout not stop infinite loop - php

Why php or apache not stop infinite loop?
while (true)
{
sleep(5);
}
Apache have default TimeOut = 300.
Php - max_execution_time = 30
But this loop can work over hour. Why?

Since sleep() happens outside of your script, it doesn't contribute to the max execution time of your script.
Note: The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), the sleep()
function, database queries, etc. is not included when determining the
maximum time that the script has been running.
http://php.net/manual/en/function.sleep.php

Related

Can I increase Apache's TimeOut directive for specific PHP script?

I have a PHP script that will be executed by requests from the application admins. It does lots of stuff and takes at least 20 minutes(depending on the database size)
the Apache TimeOut directive is set to 300(5 minutes) which closes the connection and returns 500 status code after my PHP script is finished if it takes longer time to execute
Setting the PHP ini max_execution_time for long time for this script is useless.
<?php
// long script
ini_set("max_execution_time", 3600);// 1 hour
// Apache still responses with the same "Connection: close" header and 500 status code
And I don't want to change the entire Apache TimeOut directive just for those couple of scripts, because if I did, any request will be able to take very long time which makes a scope for DDOS vulnerabilities, is this right?
Is there any way to allow this script only to run longer at the Apache level ?
Have you tried PHP's set_time_limit() method?
https://www.php.net/manual/en/function.set-time-limit.php
In addition to setting the initial execution time, the manual says that calling it resets the time expended to zero when called, and starts the counter again to the limit provided.
So if you want to be sure, you could just call set_time_limit(0) (0 == no limit) regularly throughout your script to make sure that you don't ever hit a limit (even though you're setting an infinite limit by passing in 0).

Php and max_execution_time

I need to one of my script (not all) works only five second. And if execution not finished yet (within five seconds) it should be dropped.
So I use
ini_set('max_execution_time', 5);
Also if I do
ini_get('max_execution_time');
it shows me five second, but script not interrupt after 5 seconds.
P.S
safe_mode = off
nginx -> php-fpm
set_time_limit(5) also has no effect
You can use set_time_limit function on top in your code as follows:
set_time_limit(5)
NB: If you dont place it on top of your code, supposed the php script has ran for 3 seconds and you called set_time_limit(5) so the total time of allowed execution would be 3+5 = 8 sec not 5sec as expected
Update
From php documentation:
Any time spent on activity that happens outside the execution of the
script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.

PHP max execution time wrong

One of my PHP scripts is throwing the following error at me:
Fatal error: Maximum execution time of 30 seconds exceeded
The problem is that the script has only been executing for a few seconds.
I've timed everything between 2 and 10 seconds.
set_time_limit and ini_set('max_execution_time') have no effect (because of PHP safe mode).
Now I could just increase the value in my php.ini , but I would like to know why this error is thrown within a fraction of the actual maximum execution time.
The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
Or You can user set_time_limit(0) it will help only safemode.
This means that you might have an infinite loop somewhere or there is some code that has a timeout higher than 30 seconds.

php max_execution_time value is 30seconds but run 200seconds?

im getting php maximum execution time like this.
<?php ini_get('max_execution_time'); ?>
output: 30
but i can run this script.
<?php
sleep(200);
echo "no timeout error";
?>
output: no timeout error
im how to get real ini value ?
best regards
From the manual:
The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
Since sleep is a system call, it doesn't count against the max execution time.
Your max_exec_time IS 30 seconds, but it's not timeouting because time is not running in sleep() but only when script is doing something

How do i make a Php script terminate after a fixed amout of time

How do i make a Php script terminate after a fixed amount of time
Using the max_execution_time setting in PHP.
Ie:
ini_set('max_execution_time', 30);
Would end the script after 30 seconds of execution (this is default in PHP)
You could also use:
set_time_limit(30);
See: http://php.net/manual/en/function.set-time-limit.php and http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
I would use the set_time_limit() function which specifies the amount of time a script is allowed to run after the point at which this function has been called.
More info here

Categories