Wednesday, 11 July 2012

Maximum execution time exceeded - Error in PHP

You might have come across a Fatal error stating Maximum execution time of 30 seconds exceeded when running time consuming PHP scripts. Well, PHP sets its default maximum execution time for a script to 30 seconds so that the server is not overloaded running long scripts. But there are times when you need to run some time consuming scripts that does some database maintenance or processing large amounts of data. In such cases you are likely to exceed the 30 seconds time limit. PHP has a built in function set_time_limit which can be used to change the maximum execution time.
PHP has its maximum execution time defined in the php.ini file. Changing the entry in this file would be the fastest solution but it is not recommend as this change will reflect in all the PHP scripts in the installation.
Using the set_time_limit function is the best solution to change the maximum execution time of the required script alone without affecting any other scripts.

The set_time_limit function takes one integer parameter which is the number of seconds the script should run and returns void.

void set_time_limit(int $seconds)

If you set the number of seconds to zero, then no time limit will be enforced. So the function call will be as follows.

set_time_limit(0);

Place this function call statement at the beginning of the script so that the time restriction is removed.
You can specify any integer value for the number of seconds. Each call to the set_time_limit function will restart the timeout counter from zero.

Note : This function has no effect when PHP is running in safe mode. In such cases, altering the php.ini file is the only option.

No comments:

Post a Comment