One important aspect of PHP programs is that the maximum time taken to execute a script is 30 seconds. The time limit varies depending on the hosting companies but the maximum execution time is between 30 to 60 seconds. The user may get errors of maximum time limit exceeds due to some heavy import or export of files or program which involves sending mail to many recipients. To avoid the situation, you need to increase the execution time limit.
This article describes how to change or control the maximum execution time of a PHP script.
You have already a custom php.ini file set up in your application or a request has to be done to the owner who maintains it. If some of the PHP scripts take longer time then the server stops and throws an error:
Fatal error: Maximum execution time of..seconds exceeded in this_file.php on line...
To avoid this situation, you can change the max_execution_time directive in php.ini configuration file. Let us look at the ways by which we can set time for script execution in PHP. These are listed below:
- Search for max_execution_time directive in the php.ini file and edit the value of it, as required by the PHP script.
; 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 = 4000
The default value of the directive is changed as required.
Note: We have to restart the web-server, once the changes are done in the configuration file. By this setting, the configuration is made global to all PHP scripts. Changes done to this file in an incorrect way can create problems to the web-server or live projects.
- 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. Use set_time_limit(0) when the safe mode is off.
Note: If the function is called at the very start of the program, then the value passed to the function will be the time limit for the execution of the script. Otherwise, if the function is called in the middle of the code, then the partial script is executed and then for the rest of the script, the time limit is applied.
- Use PHP inbuilt function ini_set(option, value) where the paramaters are the given configuration option and the value to be set.
// The program is executed for 3mns.
It is used when you need to override the configuration value at run-time. This function is called from your own PHP code and will only affect the script which calls this function. Use init_set(‘max_execution_time’0) when you want to set unlimited execution time for the script.
Note: Use init_set() function when the safe mode is off.
// Sets to unlimited period of time
Note: This function with ‘0’ as a parameter is not a good programming practice but can be used for developing and testing purpose. Before the code is moved to live or production mode, make sure the settings are revoked.
- For allowing to run the script forever and ignore user aborts, set PHP inbuilt function ignore_user_abort(true). By default, it set to False which throws fatal error when client aborts to stop the script.
- Use php_value command to change the settings in Apache configuration files and .htaccess files.
php_value name value
This sets the value for that particular directive as specified.
php_value max_execution_time 200
- Use of cPanel configuration setting options for changing the execution time of a script. This can be done in cPanel’s dashboard and can be used for setting the time limit for PHP script.