How to stop a PHP Script from terminating abruptly?

One can schedule PHP Scripts to run on a server using a Cron job but that does not mean it is going to run successfully. With the fact that  you may have tested the script multiple times with no run time errors being reported. Why would you think that there are uncertainties in this aspect?

PHP as you all know is a server side scripting language which is deemed to run on a server without Interruption. But if the script is running on a server that is  part of a shared hosting plan, restrictions are put in place to prevent a script from running indefinitely. This is done to prevent one tenant from consuming all the resources. And similarly if you use a 3rd party server to schedule/run your script, it would have similar rules on the table. If a script takes more than 30 seconds to run than there is a timeout and it is put on hold.

There are cases where if an end user runs a PHP Script on the browser and closes the tab/window, it would stop executing  because there has been a disconnect from the client side. As a developer we wouldn’t want this to happen. The script should run irrespective of the client’s activity status or the type of Server hosting plan that you have.

Fortunately there is an easy way to fix this! PHP has a one line code that helps in resolving this Issue. The code literally Ignores the Client’s Activity Status.



Ignore_user_abort accepts a boolean parameter. If set to true it would Ignore user action. So even if the user disconnects, the script would continue to run until it terminates on it’s own!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s