PHP connection_aborted()不总是工作 [英] PHP connection_aborted() Does not always works
问题描述
我想要下面的代码脚本在服务器和客户端之间的连接已终止时正确结束。有时工作,有时不。我不知道为什么。
示例代码如下:
<?php
ignore_user_abort(true);
register_shutdown_function('shutdown');
$ url =http://127.0.0.1:8000;
$ file_handler = @fopen($ url,rb)或die(Open failed);
foreach($ http_response_header as $ h)
{
header($ h);
}
$ bytes = 0;
while(!feof($ file_handler)和!connection_aborted())
{
$ response = stream_get_line($ file_handler,4096)
$ bytes + = strlen($ response);
echo $ response;
}
fclose($ file_handler);
function shutdown()
{
全局$ file_handler;
if(!is_null($ file_handler))
{
fclose($ file_handler);
//做一些其他代码
}
posix_kill(getmypid(),9);
}
?>
我需要做什么才能使它更准确?
感谢您
TCP要求确认所有已传送的封包因此,服务器应该检测这至少是一个发送超时...
session_write_close() / to make flush work
while(connection_status()!== 0){//如果连接正确关闭,这将工作
//或者如果只是断开连接...
睡眠(1);
echowhatever;
ob_flush();
flush();
}
First of all i read many of the topics in stackoverflow about connection_aborted before making this topic but i didn't find the solution i wanted.
I want the below script to end properly when the connection has been terminated between the server and the client. Sometime works , sometimes not. I don't know why.
The sample code is the following:
<?php
ignore_user_abort( true );
register_shutdown_function( 'shutdown' );
$url = "http://127.0.0.1:8000";
$file_handler = @fopen( $url, "rb" ) or die("Open failed");
foreach ( $http_response_header as $h )
{
header( $h );
}
$bytes = 0;
while ( ! feof( $file_handler ) and ! connection_aborted() )
{
$response = stream_get_line( $file_handler, 4096 );
$bytes += strlen( $response );
echo $response;
}
fclose( $file_handler );
function shutdown()
{
global $file_handler;
if ( ! is_null( $file_handler ) )
{
fclose( $file_handler );
//do some other code
}
posix_kill( getmypid(), 9 );
}
?>
What do i need to do to make it more accurate?
Thank you
TCP requires that ALL sent packets be acknowledged by the client and therefore the server should detect this as a send timeout at the very least...
session_write_close();//to make flush work
while (connection_status() !== 0) {//this will work if the connection is properly shutdown
//or if it is simply disconnected...
sleep(1);
echo "whatever";
ob_flush();
flush();
}
这篇关于PHP connection_aborted()不总是工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!