If you want to run a PHP script in the background and allow it complete in its own time they you will need to override the default abort signal that is sent to a php script when you hit the escape key on your browser or by hitting the stop button.
Say for example you have a PHP script is compiling reports and emails the report to a user once it is completed, the script could take a long while until it completes and you don’t want to have to stay on the page until it completes, you can allow the script to continue running even after you leave the page by setting enable the ignore_user_abort() PHP function.
You can enable the ignore_user_abort() function using the following code
<?php // ignore our abort and continue to run on the servers // even if we leave the page or hit escape. ignore_user_abort(TRUE); // optionally override the default php timeout so the script doesn't timeout ini_set('max_execution_time',600); // rest of your code follows here // this code can take ages to complete // and will run in the background once you leave the page ?&g;
Now once we leave the page and navigate somewhere else, the php script will continue to execute and do its thing. This can be very handy when a script needs to process a lot of information and you don’t want to wait around.
Overriding the PHP default timeout is recommended to stop the script from timing out. View my article on overriding php’s default timeout to see how you can do this.