0

I would like to know how do I stop the PHP process if the user closes the screen or aborts the page request? At first I found this question in stackoverflow: continue processing php after sending http response I would take care of it if it was the other way around.

I need this PHP code to run only if the screen is not aborted or closed by the user.

<?php
ob_start() ;
echo str_pad('',4096);
function flush_buffers() {
    ob_end_flush(); 
    ob_flush(); 
    flush(); 
    ob_start(); 
}  
echo ""; 
flush_buffers(); 
sleep(5); 
//execute function
setCounterVisitor(); 
?>

Can anybody help me?

Fydellys
  • 1
  • 3
  • you can use js to tell the server some one is still there, but its not that simple overall –  Jun 12 '18 at 21:02
  • I would like to implement this code in PHP. Javascript for me will not work because this code should be on my home page with no includes. Is there any solution to this case? – Fydellys Jun 12 '18 at 21:05

1 Answers1

1

If the users aborts the page request (clicking cancel, closing the tab, etc.), by default PHP stops code execution.

It can be changed using

ignore_user_abort( boolean );

If you want something to stop if the connection is closed, but the rest of the script to continue working, set ignore_user_abort(true) and then check connection_status() or connection_aborted() in the function.

Boolean is a value type that is either true (like "yes") or false (like "no").

altrisi
  • 131
  • 7
  • Friend, error it occurred: Parse error: syntax error, unexpected '( boolean )' (bool) (T_BOOL_CAST) – Fydellys Jun 12 '18 at 21:36
  • When I said `boolean` I was trying to say either `true` (ignore abort, continue execution) or `false` (stop on user abort) – altrisi Jun 12 '18 at 21:45
  • I was unable to implement this function in my script. What can it be? Would you help me? – Fydellys Jun 13 '18 at 12:11