Proper way to set php max_execution_time - php

i try to set my execution time in php, but seems like it doesn't work.
I have tried this.
<?php
function secondsToTime($s)
{
$h = floor($s / 3600);
$s -= $h * 3600;
$m = floor($s / 60);
$s -= $m * 60;
return $h.':'.sprintf('%02d', $m).':'.sprintf('%02d', $s);
}
$a = microtime(true);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('max_execution_time', 3);
sleep(2);
echo 'oke';
sleep(3);
echo 'yes';
$b = microtime(true);
echo secondsToTime($b-$a);
?>
But when i try, it shows 11 seconds, instead of error or something.
why does my code doesn't error?? i set time to 6 secs. but my whole process is 11 secs??
Do i need to define it on my php.ini too?
Thank you.

it's show me this Error
Fatal error: Maximum execution time of 6 seconds exceeded
You doesn't get this error because on your server "Display Error" is not on.
Do i need to define it on my php.ini too ?
No need to define it on php.ini too.you can set this dynamically like you already had.

set_time_limit sets the maximum execution time in seconds. If set to zero, no time limit is imposed
set_time_limit(0);
By default, the maximum execution time for PHP scripts is set to 30
seconds. If a script runs for longer than 30 seconds, PHP stops the
script and reports an error. You can control the amount of time PHP
allows scripts to run by changing the max_execution_time directive in
your php.ini file.

Related

How to set php max execution time in millisecond?

Is there any way to set php max execution time less than a second?
or
How can we handle it in code level like return a json message to show "Exceeded max allowed response time"?
No, you can't set max_execution_time less than the seocond. Also, You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini.
max_execution_time
But you can emulate it with a function as this post
declare(ticks=1);
$start = microtime(1);
register_tick_function(function () use ($start) {
(microtime(1) - $start < 0.8) or die();
});

Maximum execution time of 300 seconds exceeded

I got this error message :
And I already set this on the top of my page
// Display error - if there is
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('MAX_EXECUTION_TIME', -1);
Any suggestion ?
ini_set('max_execution_time', 0).
Don't use all caps (in general for ini settings) and use 0 not -1 when setting value to no limit.
http://php.net/manual/en/info.configuration.php#ini.max-execution-time

PHP Script stops working after 60 seconds

I'm trying to create a form just to mess around with and I there are two sections of this form. The first input is a number to count up to and the second input is a delay interval in seconds.
I have two ways of doing this. One, is to do this client side and the other is to take the variables and send them via php. This will allow the user to leave the website and have the script still working.
The issue with this being server side is that after 60 seconds, the PHP script stops working. The form is being sent via AJAX. Here is my PHP code. I'm also on shared hosting and do not have the ability to edit PHP.ini.
$amount = $_POST["amount"];
$time = $_POST["time"];
date_default_timezone_set('America/Los_Angeles');
$x = 0;
while($x < $amount) {
echo $x;
$x++;
sleep($time);
}
echo ("Counted to " . $x);
You can use max execution time on your php file, add this line in your php file.
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
You can use set_time_limit to set the max execution time, and 0 means no time limit (not recommanded for cgi, since you web server, apache/iis still have a time limit)
and if you want to change your execution time limit of all the php scripts, you can change the setting max_execution_time in the php.ini

Server gives error 500 on a loop with sleep - php

I am getting internal server error with a loop in php.
The loop basically do this:
for ($i = 1; $i <= 9; $i++) {
sleep(5);
}
it have an execution time limit set to 120 and also set the memory limit and display erros.
set_time_limit(120);
ini_set('memory_limit', '128M');
ini_set ('display_errors', 'On');
I get an 500 internal server error.
And other script in the same server stop to work while looping.
There is another Runtime Configuration In
You must change value of
max_execution_time In php.ini
usually this attribute set to 30 seconds , you can increase this to 100 seconds

PHP Max Exection Timeout Notification

Is it possible to get Max Execution timeout notification so that i can use code clean up operations before the php script stops running?
Basically, i am trying to create a script that can do some changes/modifications to my database which has huge data pile. Also, this php script execution can be paused/resumed using a filelock.
You can either use set_error_handler() or register_shutdown_function().
Edit: See also: max execution time error handling
If you set 0 to set_time_limit, it will never stop :)
You can increase the limit time inside your code using:
set_time_limit(300); //time in seconds
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
or
changing the max_execution_time parameter inside your php.ini.
The default time is 30 seconds.

Categories