1

Seems php scripts continue even after closing or stopping a page.

for example, if my script is run by /localhost/test.php

after I close the page, sometimes it continues to run. I'm pretty sure restarting apache clears it out but is there a better way to terminate the php script after it's started.

3
  • You can use die(); or exit(); to terminate any function Commented Oct 2, 2013 at 22:00
  • True. But I'm looking for something on the client side to stop the script. Commented Oct 2, 2013 at 22:19
  • Hm... how about a (GET) link to a function with an exit? All that comes to mind. Commented Oct 2, 2013 at 22:38

2 Answers 2

2

You could use ignore_user_abort but I think it only applies if you are running PHP as a command line script .

When running scripts from browser , in my experience I have realized scripts running even after the browser was closed . I did not go further to check how long they used to run .

The scripts that does huge processing that could run many minutes , I used to have control as below :

  • A flag variable is considered which is stored in a database table .
  • A way is provided to set the flag On or Off ( or 1 or 0 ) .
  • The flag is checked in the script and the script is stopped running if the flag is found to be Off ( or 0 ) .

One way to consider the flag is in a loop as below :

while(true)
{
    /* Your code here which probably does lot of processing */

    if($flag === false) break; // Or exit if you prefer
}

If you have code that need to run following the loop , you would use break .
If you want to abruptly stop the script you could use exit instead in its place .

Sign up to request clarification or add additional context in comments.

2 Comments

ah! that's really good. maybe a tad inelegant but certainly solves my issue. thanks.
No problem . I have PHP scripts that run in terms of minutes which are tested from a browser initially and later are set as a cron jobs . For my purposes I found it the best way to have control over such scripts . It's good that you found my answer helpful . Cheers ...
2

check this out: http://php.net/manual/en/function.ignore-user-abort.php

Sets whether a client disconnect should cause a script to be aborted.

1 Comment

the default is set to false (user_aborts are not ignored). and yet it still runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.