0

I'm trying, handle my program when client turn off browser or tab. while request php is not complete.

For example, i will save score to Database before browser or tab turn off

$i = 0;
$score = 0;
while($i++ < 100000){
    $score +=2;
}
$sql = "UPDATE games SET score=$score WHERE uid=123";
$conn = new Mysqli(params);
$conn->query($sql);

if current $i is 1000, request is handling and client turn off browser or tab. Therefore, below command is not handle.

$sql = "UPDATE games SET score=$score WHERE uid=123";
$conn = new Mysqli(params);
$conn->query($sql);

How to immediate, i can save current score to Database.

Tiến Mạnh
  • 117
  • 1
  • 2
  • 10

1 Answers1

0

The default PHP behavior is:

After a user closes the browser window/tab, the script will terminate if it either:

  1. reaches its the max_execution_timeout setting (a PHP environment variable), or
  2. attempts to output anything to the browser.

However, you can change this behavior by:

  1. changing the max_execution_timeout setting to 0 (to allow for unlimited runtime) and
  2. changing the ignore_user_abort setting (another PHP environment variable) to true.

See also:

SO: excellent answer for a question just like yours

PHP manual: Connection Handling

Community
  • 1
  • 1
Adam Friedman
  • 520
  • 6
  • 20