Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

4 Ways To Set Execution Time Limit in PHP

INTRODUCTION
THE TIME PROTECTION

Welcome to a tutorial on how to set the execution time Limit in PHP. So you are getting a script timeout error while trying to run an insane massive data crunching script? Well, that is actually the default PHP protection mechanism to stop scripts from hogging system resources for too long.

In this guide, we will walk through a couple of ways to increase or even remove this time limit… But it may not be the best solution, and we will also go more into that. Read on to find out!

 

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Section A
The Basics

Section B
How to Set

Extras
More Goodies

Closing
What’s Next?

SECTION A
THE BASICS

Before we go into the methods of increasing and setting the time limit – Here is what the PHP time limit is all about, and why it is a bad idea to disable it.

WHAT IS PHP TIME LIMIT?

As in the introduction, the PHP time limit is set to prevent scripts from hogging the system resources for too long. By default, the maximum execution time is set to 30 seconds. If a script runs longer than that, PHP will stop the script and throw a timeout error.

Good news is, this is a soft limit that you can change it anytime. It will be good to also know that this limit does not apply when you run PHP scripts in the command line (CLI); There are no time limits set for command line (CLI) scripts by default.

WHY IS IT BAD TO REMOVE THE TIME LIMIT?

If you just want to increase the time limit a little bit more, by all means, go ahead if it does not hurt the normal Server operations. But the whole idea of having the time limit is a safety mechanism. Can you imagine hundreds of users hammering the server to run scripts for hours? That will crash or slow the server down to a crawl for sure.

So if you are opening the server up to processing massive amounts of data for the users, I will recommend a more robust solution to control it. I.E. Instead of this “lazy set time limit”, set the massive scripts to run in the command line as a background process, and only allow one at a time. I will leave a link to my other guide on how to do this at the extras section below.

SECTION B
WAYS TO SET THE TIME LIMIT

Now that you are aware of the potential dangers for allowing long-running scripts, here are the ways to change the time limit in PHP. Do so only after careful considerations, and increase the time limit sparingly.

1) EDITING PHP.INI

php.ini
; 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=180

If you have access to the php.ini file, you can change the max_execution_time directive. But please take note that this will affect all the other PHP scripts. I will not recommend doing so unless you really know what you are doing.

2) CHANGE WITH HTACCESS

.htaccess
php_value max_execution_time 180

If you are running an Apache server, you can also change the time limit by creating a .htaccess file in the project folder. But take note again, while this will not affect all the PHP files, but this will still affect all the PHP files within the project folder itself.

3) PHP SET_TIME_LIMIT

set-time-limit-eg.php

This is my recommended method, and that is to change the time limit within the script itself. This way, all the “potential damages” will be contained within this script only.

4) PHP INI_SET

ini-set-time-eg.php

This final method is an alternative to set_time_limit. With ini_set(), you can actually change some of the php.ini settings during run time – The first parameter is the setting that you want to change, and the second is the new value.

EXTRAS
MORE GOODIES

That’s it for all the methods, and here is a small section of extras that may be useful to you.

EXTRA) SETTING THE MEMORY LIMIT

Apart from the time limit, you may also run into a memory limit problem when crunching large datasets. Thankfully, this soft limit can also be lifted in a similar manner:

php.ini
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit=128M
.htaccess
php_value memory_limit 256M
ini-set-mem-eg.php
ini_set('memory_limit','16M');

EXTRA) RUNNING BACKGROUND TASKS

It is not a good idea to open long-running scripts to the users, count on them to hit refresh a hundred times, run a hundred scripts, and crash the server. Here is how to properly run scripts in the background instead.

3 Steps to Run PHP Background Process

CLOSING
WHAT’S NEXT?

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you in your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

The post 4 Ways To Set Execution Time Limit in PHP appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

4 Ways To Set Execution Time Limit in PHP

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×