If you want to check if the user is active while you are generating a page you can use: connection_aborted()
$DONE = false;
while( !connection_aborted() && $DONE == false ){
$DONE = true;
}
If you want to check when a browser quits then you need to use Javascript and DOM. I thought <BODY onUnload="alert('bye now!')"> might work but this will be triggered on a page refresh.