当连接中止时,我尝试在会话上标记一个值,但是php函数connection_status()无法正常工作!下面是代码:
1 个不带 while 语句:
session_start();
ignore_user_abort(true);
ob_end_clean();
echo 'Testing'; //test if it has output to the browser
flush();
sleep(7);
echo "test";
flush();
if (connection_status() != CONNECTION_NORMAL){
$_SESSION['conn']='failed'; //never called
}
2 WITH while 语句:
session_start();
ignore_user_abort(true);
ob_end_clean();
echo "Testing connection handling";
while (1) {
if (connection_status() != CONNECTION_NORMAL){
$_SESSION['conn']='failed'; //worked at some time
break;
}
sleep(1);
echo "test";
flush();
}
当我使用这些代码进行测试时,我在代码执行结束之前手动取消连接。但是第一个代码不起作用,因为我m excepted(do not mark assign 'failed' to the session) , and the second code works.
Why the first dosen
不起作用?!
正是由于while
,这才有效。
一想。当您第一次运行此PHP页面时,连接不会出错,但是一段时间后,浏览器已过时并且与服务器断开连接(可能是gced),连接被归类为故障,因此您的代码可以工作。
因此,它之所以有效,是因为它循环检查连接。
老实说,我不确定您为什么要尝试在服务器和客户端之间建立这种完整性,这充其量是不可靠的。这似乎是互联网运作方式的一个根本缺陷。