How to disable max execution time in PHP

When you write heavy scripts, especially ones that scrape data or that have to load XML data for example, you might encounter the problem of your script timing out. You will see something like:

Fatal error: Maximum execution time of 30 seconds exceeded

Here´s how you can easily solve it

At the top of your script you can set the execution time, if you give it a 0 (zero) as argument, then there will be no limit.

set_time_limit(0)

Is set_time_limit set for one page or global?

For one page. When I changed this value for the first time, I thought: Am I changing this value for every page on my server? At the time of writing this post, the documentation wasn´t very clear on this, but it is only global if you change the time limit for the maximum execution time through the php.ini settings. If you set it in the script through set_time_limit, it is only for that script, the rest will use its default of usually 30 seconds.

Set_time_limit at the start of my script does not work, why?

It could be that PHP is running in safe mode. Especially if you are renting shared hosting for your script. In this case it can only be changed through the PHP.ini file (call ini_set or change the actual file), which you probably don´t have access to :(

If you have no idea what a PHP.ini file is, then I can really recommend installing your own webserver with PHP. Nowadays there are easy installers available, also for Windows, it´s not difficult at all. Just download and install Apache webserver and PHP. Find some tutorial if you run into any problems. Doing this will teach you a lot about PHP and webservers in general, and developing on your own machine can save you a lot of time as well.

Victor

Living in Europe as a freelance programmer. Interested in programming in general, web development and machine learning in particular. Currently most used language is PHP, also experienced in JAVA.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>