PHP detect if the user closes the browser method

1. Example 1

echo str_repeat ( "", 3000); 
the ignore_user_abort (to true); 
MyLog ( 'Online'); 
the while (to true) { 
   / * 
   *. 1, the program ends normally the connection_status 0 
   * 2, click on the browser "stop" button the connection_status. 1 
   *. 3 , 2 overtime the connection_status 
   * / 
 echo "the Test <br> \ the n-"; // Note that the program must be output, otherwise undetectable ABORTED state is 
 flush (); 
 SLEEP (1); 
 ! IF (the connection_status () = 0 ) { 
    MyLog ( 'Offline'); 
    Die ( 'End The Script'); 
 } 
} 
function MyLog (STR $) 
{ 
   $ FP = the fopen ( 'E: /abort.txt', 'A'); 
   $ = DATE STR ( 'H Ymd: I: S') STR $ "\ R & lt \ n-";.. 
   fwrite (FP $, $ STR); 
   fclose ($ FP);
}

  

2. Example 2

function foo() {
 $s = 'connection_status '. connection_status();
 mylog($s);
} 
register_shutdown_function('foo');//script processing is complete or when exit() is called
set_time_limit(10);
for($i=0; $i<10000000; $i++)
 echo $i;
function mylog($str)
{
  $fp = fopen('e:/abort.txt', 'a');
  $str = date('Y-m-d H:i:s').$str."\r\n";
  fwrite($fp, $str);
  fclose($fp);
}

  

 

Guess you like

Origin www.cnblogs.com/cblx/p/12543980.html