dsgdg54ef4365 2012-08-09 09:15 采纳率: 0%
浏览 50

PHP connection_status无法正常工作

I try to mark a value on session when the connection is aborted, but the php function connection_status() dosen`t work correctly! Here is code below:

1 WITHOUT while statements:

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 statements:

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(); 
}

When I test with these code, I manually abord the connection before end of the code excution. But first code do not work as Im excepted(do not mark assign 'failed' to the session) , and the second code works. Why the first dosent work?!

  • 写回答

1条回答 默认 最新

  • duanfa2014 2012-08-09 13:27
    关注

    It is because of the while that this works.

    Think about it for a second. When you first run this PHP page the connection will not be faulty but after some time and the browser has become stale and disconnected (gced maybe) from the server the connection is classed as faulty and as such your code works.

    As such it works because it loops to check the connection.

    To be honest I am not sure why you are trying to create such integrity between the sever and the client, this would be unreliable at best. It seems like a fundamental flaw in how the internet works.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100