35

I'm having a problem with my PHP file that takes more than 30 seconds to execute.

After searching, I added set_time_limit(0); at the start of the code,cbut the file still times out with a 500 error after 30 seconds.

log: PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /xxx/xx/xxx.php

safe-mode : off
Alex Kalicki
  • 1,533
  • 9
  • 20
user614963
  • 823
  • 3
  • 9
  • 13
  • 7
    Are you on shared hosting? php functions can be selectively disabled/restricted by server configuration. – Marc B Aug 17 '12 at 04:17
  • 2
    can you `echo ini_get('max_execution_time');` after your `set_time_limit(0);` and tell what you get? – Prasanth Aug 17 '12 at 04:20
  • There are may be `php_admin_value max_execution_time XX` set in httpd.conf. So you can't override this value. – verybadbug Oct 01 '13 at 23:49

6 Answers6

43

Check the php.ini

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

ini_set('max_execution_time', 0); //0=NOLIMIT
user1601782
  • 441
  • 3
  • 4
11

This is an old thread, but I thought I would post this link, as it helped me quite a bit on this issue. Essentially what it's saying is the server configuration can override the php config. From the article:

For example mod_fastcgi has an option called "-idle-timeout" which controls the idle time of the script. So if the script does not output anything to the fastcgi handler for that many seconds then fastcgi would terminate it. The setup is somewhat like this:

Apache <-> mod_fastcgi <-> php processes

The article has other examples and further explanation. Hope this helps somebody else.

Paul
  • 1,056
  • 11
  • 18
5

I usually use set_time_limit(30) within the main loop (so each loop iteration is limited to 30 seconds rather than the whole script).

I do this in multiple database update scripts, which routinely take several minutes to complete but less than a second for each iteration - keeping the 30 second limit means the script won't get stuck in an infinite loop if I am stupid enough to create one.

I must admit that my choice of 30 seconds for the limit is somewhat arbitrary - my scripts could actually get away with 2 seconds instead, but I feel more comfortable with 30 seconds given the actual application - of course you could use whatever value you feel is suitable.

Hope this helps!

SteJ
  • 1,491
  • 11
  • 13
3
ini_set('max_execution_time', 300);

use this

Anooj P
  • 346
  • 4
  • 16
3

Checkout this, This is from PHP MANUAL, This may help you.

If you're using PHP_CLI SAPI and getting error "Maximum execution time of N seconds exceeded" where N is an integer value, try to call set_time_limit(0) every M seconds or every iteration. For example:

<?php

require_once('db.php');

$stmt = $db->query($sql);

while ($row = $stmt->fetchRow()) {
    set_time_limit(0);
    // your code here
}

?>
Vins
  • 8,849
  • 4
  • 33
  • 53
  • 2
    Wait... What? No? `set_time_limit()`: A) Resets the timer on every call (good, and I see where you were going). B) imposes no time limit when set with 0. So... No... – Mike Apr 11 '14 at 21:27
  • This should be marked as the correct answer. Most likely you will be using loops and the set_time_limit(0) command should be at the top of the loop that causes the timeout. The parameter 0 means that no time limit will be imposed (as per the manual), therefore the script can run until completion. However if your script is taking longer than 30 seconds to run, you may wish to reconsider how you are doing things? – kurdtpage Feb 15 '16 at 02:36
  • 1
    From the [manual](http://php.net/manual/en/function.set-time-limit.php) **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(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. – Noman Riffat Feb 20 '19 at 05:22
1

I think you must say limit time to execution to php , try this.

ini_set('max_execution_time', 0);
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
christian
  • 39
  • 6