Override The Default PHP Execution Timeout Limit

Overview

In many 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 dataset 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 server’s 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. You can even remove the time limit although this isn’t generally recommended. 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.

Override The Default PHP Execution Timeout Limit

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. Want to remote the time limit? Setting the limit to 0 (zero) will remove the PHP execution timeout and possibly cause the PHP script to run forever.

max_execution_time = 600

 

Setting the PHP execution timeout using set_time_limit()

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

<?php

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

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

?>

 

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.

<?php

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

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

?>

 

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.

# set timeout to 10 minutes
php_value max_execution_time 600

Final Words

Increasing the timeout limit can be very useful but should be used with caution. Removing the time limit can be done by setting the limit to 0, but again, use with caution. Timeout limits are there for a reason and should only be used when other options are not available or viable.