How can I set the maximum execution time for a PHP script?

43,792

Solution 1

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.

Solution 2

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

Share:
43,792

Related videos on Youtube

Nick Q.
Author by

Nick Q.

Working with hackers to make their lives even more awesome. Currently, COO at Major League Hacking. Former Developer Evangelist at SendGrid. Unabashed nerd & computer lover. Hit me up on Twitter: @YayNickQ

Updated on July 25, 2020

Comments

  • Nick Q.
    Nick Q. almost 4 years

    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?

    • Marc B
      Marc B about 13 years
      Apache's AllowOverride has nothing to do with PHP settings.
    • Frank Farmer
      Frank Farmer about 13 years
      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); ?
    • Nick Q.
      Nick Q.
      I forgot to mention that I put a php_value max_execution_time 1000 in .htaccess, which is why I mentioned AllowOverride.
    • Jeff Busby
      Jeff Busby
      have you tried setting it right in the php.ini file?
  • mlwacosmos
    mlwacosmos almost 7 years
    is there a security issue modifying this directive in the Virtual Host ?
  • Lukas
    Lukas almost 7 years
    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 ma.ttias.be/…. You might want to be careful about allowing long runtimes in a whole VirtualHost though. Maybe use it inside a <Location> tag to be more granular.
  • mlwacosmos
    mlwacosmos almost 7 years
    Can you tell me what to write using the location tag ?
  • Lukas
    Lukas almost 7 years

Related