php判断页面是否关闭的方法:可以利用connection_status()函数来进行判断。如果该函数返回0,则表示连接运行正常;如果返回1,则表示连接由用户或网络错误终止。

相关函数介绍:
connection_status() 函数返回当前的连接状态。
(推荐教程:php视频教程)
可返回的可能值:
立即学习“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<br>\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培训
以上就是php如何判断页面是否关闭的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号