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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用