11

I would like to change the maximum execution time for a PHP script. In the script I have tried

ini_set("max_execution_time", "1000");

and

set_time_limit(1000);

together and separately.

I also added this line to .htaccess:

php_value max_execution_time 1000

php.ini has safemode off and the Apache server has the flag AllowOverride All. What must I do to get the server to allow a longer execution time?

TRiG
  • 10,148
  • 7
  • 57
  • 107
Nick Q.
  • 3,947
  • 2
  • 23
  • 37
  • 1
    have you tried setting it right in the php.ini file? – Jeff Busby Apr 12 '11 at 16:27
  • 3
    Apache's AllowOverride has nothing to do with PHP settings. – Marc B Apr 12 '11 at 16:46
  • http://br.php.net/manual/en/configuration.changes.php try set and after get the value to check it... – Felipe Cardoso Martins Apr 12 '11 at 17:13
  • 1
    I forgot to mention that I put a `php_value max_execution_time 1000` in `.htaccess`, which is why I mentioned AllowOverride. – Nick Q. Apr 12 '11 at 17:31
  • Can you describe the exact nature of the problem you are experiencing? Is it timing out? If so, after so long? Does it happen to be exactly 300 seconds? – Charles Apr 12 '11 at 20:39
  • I want to run a script that takes an exceedingly long time to run, it starts running and the results can be seen, however, it stops running after approximately thirty seconds and returns error 500, although I haven't ever tested the exact time. As for setting it in php.ini because it is just one script and not the whole site. – Nick Q. Apr 12 '11 at 21:15
  • 2
    Are you sending requests directly to apache? Proxies may time out even if apache doesn't. What do you get if you run `var_dump(ini_get('max_execution_time'), ini_get('safe_mode'));` after your call to `set_time_limit(1000);` ? – Frank Farmer Apr 13 '11 at 00:37

3 Answers3

7

Setting the variable in the ini file works for me:

max_execution_time = 1000;

set_time_limit() should work as well, as long as it's not in safe mode.

Bryan Agee
  • 4,924
  • 3
  • 26
  • 42
6

If you're looking for an Apache2-directive to use in .htaccess or the configuration of one VirtualHost, you probably need php_admin_value:

php_admin_value max_execution_time 1000

AFAIK this only works with mod_php

Lukas
  • 161
  • 1
  • 2
  • is there a security issue modifying this directive in the Virtual Host ? – mlwacosmos Jul 18 '17 at 11:51
  • I wouldn't think so. Basically there is a difference between php_admin_value and php_value, the latter permits ini_set() to change the value, the former doesn't. see https://ma.ttias.be/php-php_value-vs-php_admin_value-and-the-use-of-php_flag-explained/. You might want to be careful about allowing long runtimes in a whole VirtualHost though. Maybe use it inside a tag to be more granular. – Lukas Jul 18 '17 at 14:52
  • Can you tell me what to write using the location tag ? – mlwacosmos Jul 19 '17 at 06:46
  • 1
    Have a look at https://httpd.apache.org/docs/current/mod/core.html#location – Lukas Jul 19 '17 at 08:51
-2

Note: This hack only to run a particular script for a certain time.

Generally you should change php setting.

So In run time you can set

set_time_limit($seconds); // 0 for unlimited

at the beginning of a script.

infomasud
  • 2,263
  • 1
  • 18
  • 12