Increase PHP Script Max Execution Time Limit Using ini_set Function

We’ve all came 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 a 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.

Increase max execution time for a single PHP script

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 a 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 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 the 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 –

Summary

Interesting Links

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 life saver, but if used incorrectly, is a ticking-time bomb for your web-server.

Increase PHP Script Max Execution Time Limit Using ini_set Function

You May Also Like

Leave a Reply

Your email address will not be published.