PHP connection_aborted() Does not always works

蹲街弑〆低调 提交于 2019-12-02 13:25:28

The meaning connection_aborted() is, that the server knows, that the connection to the client has been lost. As it happens, this very often is not the case:

  • Think of the client unplugging the network cable, walking out of WiFi coverage, driving into a underground carpark with his mobile: No packet that says, that the connection was terminated is ever communicated between the client and the server. This means, that the server will learn about the connection dropping only, when sending the reply is unsuccessfull - possibly long after your script has ended.
  • Think of a busy server (network-busy): It is quite likely, that the connection.ending packet is received and processed at the webserver only after your script has gone on running for an appreciable time.

Long story short: It is a property of TCP, that loss of connection might ot might not be detected immediately.

TCP requires that ALL sent packets be acknowledged by the client and therefore the server should detect this as a send timeout at the very least...

session_write_close();//to make flush work
while (connection_status() !== 0) {//this will work if the connection is properly shutdown
                                   //or if it is simply disconnected...
  sleep(1);
  echo "whatever";
  ob_flush();
  flush();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!