Webby
Webby

Reputation: 2765

How to stop ignore_user_abort once started?

Whats the best solution to stop a ignore_user_abort actively running on the server in a loop? (PHP)

Basically trying to stop a infinitely looping PHP script that won't stop because ignore_user_abort was set and the script executed ..

Simple exaggerated example :

<? 

ignore_user_abort(true);

set_time_limit(0);

$test = 1;

while ($test < 1000000000000000 )

{

sleep(1);
$test = $test+1;
}

?>

Upvotes: 0

Views: 3245

Answers (2)

sneha
sneha

Reputation: 11

//this will stop the script after running for  30 sec
set_time_limit(30);

Upvotes: 1

user895378
user895378

Reputation:

It seems connection_aborted is what you're looking for?

<?php
ignore_user_abort(TRUE);
set_time_limit(0);

while (TRUE) {
  if(connection_aborted()) {
      break;
  }
  sleep(5);
}

// do some stuff now that we know the connection is gone
?>

Alternatively you could check connection_status against the predefined CONNECTION_ABORTED constant:

if(connection_status() == CONNECTION_ABORTED)
{
    break;
}

You can get a good rundown in the manual docs on Connection handling

Upvotes: 2

Related Questions