How do we set an infinite execution time for php script?

This article describes how to set the maximum execution time for PHP scripts by using the max_execution_time directive in an .htaccess file.

The information in this article only applies to certain types of hosting accounts. To determine whether or not the information below applies to your account, please see this article.

This article assumes that you have already set up a custom .htaccess file. If you have not already set up a custom .htaccess file, please read this article first.

Using the max_execution_time directive

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in an .htaccess file.

To change the maximum execution time for your PHP scripts, follow these steps:

  1. Log in to your account using SSH.
  2. Use a text editor to add the following line to the .htaccess file. Replace 30 with the maximum execution time value that you want to set, in seconds:
    php_value max_execution_time 30
  3. Save the changes to the .htaccess file and exit the text editor.
  4. To verify that the new setting is active, create a PHP test file that contains the following code in the same directory where the .htaccess file is located:
    <?php phpinfo(); ?>
  5. Load the test file in your web browser, and then search for the name of the directive. The Local Value column should display the new setting that you specified in the .htaccess file.

More Information

  • To view a complete list of PHP directives, please visit http://www.php.net/manual/en/ini.list.php.
  • For more information about the max_execution_time directive, please visit http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time.

  1. HowTo
  2. PHP Howtos
  3. Set Maximum Execution Time in PHP

Created: May-11, 2022

  1. Use the ini_set() Function to Set the Maximum Execution Time of a Script in PHP
  2. Use the set_time_limit() Function to Set Maximum Execution Time of a Script in PHP
  3. Change the max_execution_time Option in php.ini to Set Maximum Execution Time of a Script in PHP

In PHP, the execution timeouts after a certain period are generally thirty seconds. So, it is a hassle to process a huge file.

But, we can set the maximum execution time of the script by ourselves. This article will introduce three ways of setting the maximum execution time of a script in PHP so that big files will be processed easily.

Use the ini_set() Function to Set the Maximum Execution Time of a Script in PHP

We can use the ini_set() function to set the execution time of a PHP script. The function is used to set the value of PHP configurations.

The first parameter takes the configuration name, while the second parameter is the value for the configuration. The configuration name should be a string.

PHP has a configuration max_execution_time responsible for defining the script’s maximum execution time. We can use the configuration option in the ini_set() function to set our desired execution time.

For example, write the ini_set() function at the top of the PHP script. Write 'max_execution_time' as the first parameter and '240' as the second parameter.

Consequently, the execution time has been set to 240 seconds, equivalent to four minutes. We can also set the second parameter to '0' to set the execution time to infinite.

Thus, we can use the ini_set() function to set the execution time of the current PHP script.

Example Code:

ini_set('max_execution_time', '240');

Use the set_time_limit() Function to Set Maximum Execution Time of a Script in PHP

We can also set the execution time of a PHP script using the set_time_limit() function.

The function sets the maximum execution time of a script. It takes an integer parameter in seconds.

For example, write the function set_time_limit() and the integer 240 as its parameter. As a result, the maximum execution time of the current PHP script is set to four minutes.

However, we need to turn the safe_mode configuration option off in php.ini to use the function. It is because set_time_limit() can potentially compromise the server security.

For example, if we set set_time_limit(0), an infinite loop can run forever in the script as there is no bound to maximum execution time. To turn safe_mode off, we can add the line safe_mode = Off in the php.ini file.

We can set the script’s maximum execution time in PHP using the set_time_limit() function.

Example Code:

set_time_limit(240);

Change the max_execution_time Option in php.ini to Set Maximum Execution Time of a Script in PHP

The methods we discussed above set the maximum execution time of the script temporarily. That is, when the script ends, the configuration is also terminated.

But, there is a way of permanently setting the maximum execution time of all the PHP scripts. We need to change the configuration option max_execution_time with our desired value in the php.ini file.

We can locate the php.ini file by navigating the output of the phpinfo() function.

The example below sets the maximum execution time to 240, equivalent to four minutes.

Example Code:

max_execution_time = 240
How do we set an infinite execution time for php script?

How do you set an infinite execution time for a PHP script and avoid the error maximum execution time exceeded '?

Use PHP inbuilt function set_time_limit(seconds) where seconds is the argument passed which is the time limit in seconds. It is used, when the user changes the setting outside the php. ini file. The function is called within your own PHP code.

How can we increase the execution time of PHP script?

To increase the script execution time, you can use the following snippet at the top of your script. ini_set ( 'max_execution_time' , '300' ); In the above example, it would set the max_execution_time directive to 300 seconds.

How do I change maximum execution time?

Set Max_Execution_Time globally in php..
Locate and open your PHP build folder..
Find the php.ini file and open it..
Find the following line in the text configuration file – max_execution_time=30..
Change the value 30 to the value of choice, Remember, this value is in seconds..
Save & Close..

What is Max execution time in PHP?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.