php判断页面是否关闭的方法:可以利用connection_status()函数来进行判断。如果该函数返回0,则表示连接运行正常;如果返回1,则表示连接由用户或网络错误终止。
相关函数介绍:
connection_status() 函数返回当前的连接状态。
(推荐教程:php视频教程)
可返回的可能值:0 - CONNECTION_NORMAL - 连接运行正常
1 - CONNECTION_ABORTED - 连接由用户或网络错误终止
2 - CONNECTION_TIMEOUT - 连接超时
3 - CONNECTION_ABORTED & CONNECTION_TIMEOUT
代码实现:echo str_repeat(" ",3000);
ignore_user_abort(true);
mylog('online');
while (true) {
/*
* 1、程序正常结束 connection_status 0
* 2、点击浏览器“停止”按钮 connection_status 1
* 3、超时 connection_status 2
*/
echo "test
\n"; //注意程序一定要有输出,否则ABORTED状态是检测不到的
flush();
sleep(1);
if (connection_status()!=0){
mylog('offline');
die('end the script');
}
}
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);
}
相关推荐:php培训