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 dosen
t work?!
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.