Check if user closed the page in PHP?

前端 未结 9 2005
孤城傲影
孤城傲影 2020-12-22 06:04

I made a chat using PHP and JavaScript chat and there is a disconnect button which removes user from the chat removing him from user list first. But if the user closes brows

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-22 06:48

    Request a PHP script that goes a little something like this, with AJAX:

    register_shutdown_function("disconnect_current_user");
    header('Content-type: multipart/x-mixed-replace; boundary="pulse"');
    
    while(true) {
       echo "--pulse\r\n.\r\n";
       sleep(2);
    }
    

    This way, you won't constantly be opening/closing connections.

提交回复
热议问题