Ini_set max_execution_time php

The max_execution_time directive defines the maximum execution time for your PHP scripts and applications. If not configured, the default maximum execution time for a PHP script is 30 seconds. PHP will timeout and exit with a fatal error once it reaches the time limit.

Ini_set max_execution_time php

Fatal error: Maximum execution time of 30 seconds exceeded in yourscript.php

You can set PHP's time limit by configuring max_execution_time option so that your script won't timeout and exit with an error.

There are a few methods to set the option, such as configuring the php.ini file, using ini_set() and set_time_limit() functions, and using a .htaccess file. cPanel, on the other hand, allows setting up the max_execution_time option from the web interface.

Methods to increase maximum execution time limit for PHP:

Change PHP maximum execution time limit in php.ini

This approach will affect all the PHP scripts running in your system. It's the best solution if you're running a content management system or framework like WordPress or Drupal as you don't need to configure the option manually to all the affected scripts.

Set the max_execution_time in your PHP configuration file to the number of seconds that want to allow your PHP scripts to execute.

  1. Open php.ini file using your favourite text editor.

    $ sudo vi /etc/php/7.2/apache2/php.ini

  2. Look for max_execution_time directive.

    ; Maximum execution time of each script, in seconds
    ; http://php.net/max-execution-time
    ; Note: This directive is hardcoded to 0 for the CLI SAPI
    max_execution_time = 30

  3. Set the value for max_execution_time in seconds.

    max_execution_time = 300

    Setting it to 0 will impose no time limit whatsoever to the execution of your PHP scripts.

    It's not recommended to set the value to 0 at this level as this method's system-wide change will cause any misbehaved PHP scripts to unnecessarily consume precious CPU time.

  4. Restart your web server.

    $ sudo systemctl restart apache2

Change PHP maximum execution time limit via ini_set() function

It is basically the same as the previous solution, but it's actually a function you call from your PHP script. It's a general function to override any configuration options set in your PHP's configuration file and only affect the execution of the scripts that call the function.

When placed at the start of your PHP script, the following function call will allow it to run for 300 seconds (5 minutes).

ini_set('max_execution_time', 300);

The function call should be placed in shared files such as the configuration files for CMS and frameworks. For WordPress it's wp-settings.php.

Ini_set max_execution_time php

Change PHP maximum execution time limit via set_time_limit() function

It is a PHP built-in function specifically to set the maximum execution time limit of your PHP scripts. It's to be called from your PHP script as in the previous method, and the following example is to also set the limit to 300 seconds (5 minutes).

set_time_limit ( 300 );

Change PHP maximum execution time limit in .htaccess

This is the best option if you don't have administrator access to your system but still want to set the option to all your scripts in your project folder.

Add the following lines to .htaccess file within your PHP project folder, and this will be applied to all the scripts within that folder.

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

Use mod_php5.c if you're hosting on PHP 5.

htaccess is an Apache-specific solution.

Change PHP maximum execution time limit for cPanel hosting

The option is readily available in cPanel's dashboard and can be used to change PHP's maximum execution time limit.

We’ve all come across the dreaded fatal error of max execution time once or twice, it can be a pain. PHP’s maximum execution time is set at 30 seconds as a bare default, but in some cases, it needs to be higher. PHP has your back though, you can change the default settings by using one function; ini_set(). It takes two parameters, a string, and an integer. The first is the exact setting you want to change, and the second is the number of seconds you want to set that setting at.

To quickly fix your issues for heavy scripting, add the following line of PHP to the very top of your script.

Two important points to note –

  • The setting change must be done at the very beginning of the script, otherwise, you risk executing code against the default and the newly set time, which isn’t going to turn out well.
  • This will only apply to the page you add the line of code too.
ini_set('max_execution_time', 120); // 120 (seconds) = 2 Minutes

Setting an unlimited maximum executing period

If you’re not happy with specifying an exact number of seconds, you can have it set to unlimited, meaning there is no maximum time of execution. This, of course, is kinda bad practice, but if it’s for development and testing purposes, it’s pretty handy. Just make sure you revoke the setting before you push your changes live again.

ini_set('max_execution_time', 0); // 0 = Unlimited

Set Max_Execution_Time globally in php.ini

You can issue this setting to all of your projects pages or scripts by using the global PHP.ini file. By changing it’s a default setting to whatever you require, then it is possible that you won’t really need to set it in the script anymore. To change your execution time in php.ini follow the steps below

  1. Locate and open your PHP build folder
  2. Find the php.ini file and open it
  3. Find the following line in the text configuration file – max_execution_time=30
  4. Change the value 30 to the value of choice, Remember, this value is in seconds.
  5. Save & Close.
  6. Restart your web server for changes to take effect.

References

Take a look at the documentation of both in-built PHP-functions that were talked about in this article –

  • Ini_Set function
  • Configuration Types

Summary

Changing this setting shouldn’t be done so impetuously, think of the drawbacks first, especially if it’s going to be on a live project. This little setting can be a lifesaver, but if used incorrectly, is a ticking-time-bomb for your web-server.

Post navigation

What is max_execution_time in PHP?

max_execution_time int. This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30 . When running PHP from the command line the default setting is 0 .

What is the use of Ini_set () in PHP?

The ini_set() function allows you to change system attributes that affect the way your script is executed. Changes only affect the current script, and will revert back when the script ends. To use ini_set() , pass it the value you want to change as its first parameter, and the new value to use as its second parameter.

How do I fix timeout error in PHP?

You can set PHP's time limit by configuring max_execution_time option so that your script won't timeout and exit with an error..
Open php. ini file using your favourite text editor. ... .
Set the value for max_execution_time in seconds. ... .
Restart your web server..

How can I limit time in PHP?

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.