60

I have added set_time_limit(0); function to increase execution time but its executing only 2-3 minutes maximum.

error_reporting(E_ALL);
error_reporting(1);
set_time_limit(0);

I want to search links from a site which is taking a long time.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bajrang
  • 8,361
  • 4
  • 27
  • 40

8 Answers8

222

PHP file (for example, my_lengthy_script.php)

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

.htaccess file

<IfModule mod_php5.c>
php_value max_execution_time 300
</IfModule>

More configuration options

<IfModule mod_php5.c>
php_value post_max_size 5M
php_value upload_max_filesize 5M
php_value memory_limit 128M
php_value max_execution_time 300
php_value max_input_time 300
php_value session.gc_maxlifetime 1200
</IfModule>

If wordpress, set this in the config.php file,

define('WP_MEMORY_LIMIT', '128M');

If drupal, sites/default/settings.php

ini_set('memory_limit', '128M');

If you are using other frameworks,

ini_set('memory_limit', '128M');

You can increase memory as gigabyte.

ini_set('memory_limit', '3G'); // 3 Gigabytes

259200 means:-

( 259200/(60x60 minutes) ) / 24 hours ===> 3 Days

More details on my blog

ITAdminNC
  • 219
  • 1
  • 10
Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
  • 1
    Hilariously, this works for Suhosin "hardened" PHP environments also. – Michael Robinson Jun 25 '13 at 05:00
  • will this work on live server where i am hosting my website i have tried out but it didnt . so i am confuse that may the host provider has set there max_execution_time to 30 seconds may be thats why the example didnt work – Aitazaz Khan Jun 30 '15 at 23:52
  • 1
    @SumithHarshan , Will this, ini_set('max_execution_time', 300); //300 seconds = 5 minutes work for particularly for a script ( One time during script run ), OR will it change the default value in php.ini ( permanently) as well ??? – Hytool Aug 12 '15 at 06:00
  • 1
    OK, Got it, ini_set Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending. Thank you anyway ! – Hytool Aug 12 '15 at 06:02
10

Add these lines of code in your htaccess file. I hope it will solve your problem.

<IfModule mod_php5.c>
php_value max_execution_time 259200
</IfModule>
Community
  • 1
  • 1
Salman Aslam
  • 761
  • 1
  • 11
  • 20
3

Well, since your on a shared server, you can't do anything about it. They usually set the max execution time so that you can't override it. I suggest you contact them.

Mob
  • 10,958
  • 6
  • 41
  • 58
1

Use mod_php7.c instead of mod_php5.c for PHP 7

Example

<IfModule mod_php7.c>
   php_value max_execution_time 500
</IfModule>
1

well, there are two way to change max_execution_time.
1. You can directly set it in php.ini file.
2. Secondly, you can add following line in your code.

ini_set('max_execution_time', '100')
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

This is old question, but if somebody finds it today chances are php will be run via php-fpm and mod_fastcgi. In that case nothing here will help with extending execution time because Apache will terminate connection to a process which does not output anything for 30 seconds. Only way to extend it is to change -idle-timeout in apache (module/site/vhost) config.

FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.0-fpm.sock -idle-timeout 900 -pass-header Authorization

More details - Increase PHP-FPM idle timeout setting

Community
  • 1
  • 1
seven
  • 2,388
  • 26
  • 28
0

Try to set a longer max_execution_time:

<IfModule mod_php5.c>
    php_value max_execution_time 300
</IfModule>

<IfModule mod_php7.c>
    php_value max_execution_time 300
</IfModule>
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
0

There's probably a limit set in your webserver. Some browsers/proxies will also implement a timeout. Invoking long running processes via an HTTP request is just plain silly. The right way to solve the problem (assuming you can't make the processing any faster) is to use the HTTP request to trigger processing outside of the webserver session group then poll the status via HTTP until you've got a result set.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • I wonder if an example can be provided for this answer? Do you mean a process that is not an http request? Something like an executable? – TARKUS Jun 05 '16 at 14:21
  • "something like an executable?" - doesn't make a lot of sense. An example would be too complex for this forum - one approach is to use a daemon, another is to create a dissociated process ( http://symcbean.blogspot.co.uk/2010/02/php-and-long-running-processes.html ). I did have a look around for good daemon examples - but only found lots of bad ones (hint: should close its stdio, fork and setsid) – symcbean Jun 06 '16 at 11:23