PHP: disable PHP Timeout

By default, php scripts will timeout after 30 seconds. While not recommended, you can override this setting with the set_time_limit() command. Useful if you are using php to process multiple files, etc.

code snippet:

<?php
// Disable PHP Timeout example
// http://php.snippetdb.com
//set php script timeout, 0 to disable
set_time_limit(0);
// your time consuming code
//don't forget to reset to 30 seconds.
set_time_limit(30);
?>

notes:

Important: See official documentation for additional info, for example: set_time_limit() has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini.

 

In any programming language there is always a chance that your code will enter an infinite loop and get stuck for a long time. There is also a chance that a script may be processing large and complex data which causes it consume large amounts of server resources for a long time which affects other programs or websites that are running on the same server. In most cases this would be useful, however sometimes you will need to allow the script to complete even if it takes a long time and consumes a chunk of a servers resources.

If this is the case, you can override the default PHP execution limit, which is generally around 30 seconds and set it to a much higher (or lower) timeout limit. As you will see, PHP has introduced a PHP Execution Timeout which basically limits the amount of time a PHP script is allowed run until it is cancelled by the PHP server.

I will show you a number of ways to extend the maximum execution time of a PHP script.

Setting the PHP execution limit in php.ini

Edit your php.ini file which is usually under ‘/etc/php5/apache2/php.ini’ in Debian Linux and scroll to the line which has the max_execution_time option. Setting the limit to 0 (zero) will remove the PHP execution timeout and possibly cause the PHP script to run forever.

  1. max_execution_time = 30
Setting the PHP execution timeout using set_time_limit()

You can override the PHP execution time directly in a PHP script by using theset_time_limit() function. Setting the limit to 0 (zero) will remove the PHP execution timeout and possibly cause the PHP script to run forever.

  1. <?php

  2. // set the PHP timelimit to 10 minutes
  3. set_time_limit(600);

  4. // rest of your code will be able to run for the next 10 minutes before timing out

  5. ?>
Changing the PHP execution time using ini_set()

Just as above, you can also override the PHP timeout limit in a PHP script by using the PHP ini_set() function. Setting the limit to 0 (zero) will remove the PHP execution timeout and possibly cause the PHP script to run forever.

  1. <?php

  2. // set the PHP timelimit to 10 minutes
  3. ini_set('max_execution_time',600);

  4. // rest of your code will be able to run for the next 10 minutes before timing out

  5. ?>
Override PHP execution time in .htaccess file

Overriding the default PHP execution timeout can also be done inside an .htaccess file by using the php_value directive and using it to set the value of max_execution_time. Setting the limit to 0 (zero) will remove the PHP execution timeout and possibly cause the PHP script to run forever.

  1. # set timeout to 10 minutes
  2. php_value max_execution_time 600

Leave a comment