作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
ignore_user_abort(true);
while(!connection_aborted()) {
// do stuff
}
echo chr(0);
flush();
while(true) {
// do stuff
}
最佳答案
我参加这个聚会有点晚了,但我刚刚遇到了这个问题,并把它弄清楚了。这里发生了很多事情——这里提到了其中的一些:
PHP doesn't detect connection abort at all
要点:为了 connection_aborted()
为了工作,PHP 需要尝试向客户端发送数据。
输出缓冲器
如前所述,PHP 在尝试实际向客户端发送数据之前不会检测到连接是否已断开。这不像做 echo
那样简单。 , 因为 echo
将数据发送到任何 output buffers
可能存在,并且 PHP 不会尝试真正的发送,直到这些缓冲区足够满为止。我不会详细介绍输出缓冲,但值得一提的是,可以有多个嵌套缓冲区。
无论如何,如果您想测试 connection_abort(),您必须首先结束所有缓冲区:
while (ob_get_level()){ ob_end_clean(); }
echo "Something.";
flush();
// returns expected value...
// ... but only if ignore_user_abort is false!
connection_aborted();
flush()
时 PHP 会做什么。被调用,并且用户中止了连接(例如:点击浏览器中的 STOP 按钮)。
flush()
基本上什么都不做。
flush
如上所述,定期向用户发送。清除所有输出缓冲区、回显、刷新。ignore_connection_aborted
是真的,需要手动测试connection_aborted()
每次冲洗后。ignore_connection_aborted
是假的,调用flush
将导致关闭过程开始。 然后你必须特别小心不要导致flush
从你的关机函数中 ,否则 PHP 将立即停止执行该函数并继续执行下一个关闭函数。 class DestructTester {
private $fileHandle;
public function __construct($fileHandle){
// fileHandle that we log to
$this->fileHandle = $fileHandle;
// call $this->onShutdown() when PHP is shutting down.
register_shutdown_function(array($this, "onShutdown"));
}
public function onShutdown() {
$isAborted = connection_aborted();
fwrite($this->fileHandle, "PHP is shutting down. isAborted: $isAborted\n");
// NOTE
// If connection_aborted() AND ignore_user_abort = false, PHP will immediately terminate
// this function when it encounters flush. This means your shutdown functions can end
// prematurely if: connection is aborted, ignore_user_abort=false, and you try to flush().
echo "Test.";
flush();
fwrite($this->fileHandle, "This was written after a flush.\n");
}
public function __destruct() {
$isAborted = connection_aborted();
fwrite($this->fileHandle, "DestructTester is getting destructed. isAborted: $isAborted\n");
}
}
// Create a DestructTester
// It'll log to our file on PHP shutdown and __destruct().
$fileHandle = fopen("/path/to/destruct-tester-log.txt", "a+");
fwrite($fileHandle, "---BEGINNING TEST---\n");
$dt = new DestructTester($fileHandle);
// Set this value to see how the logs end up changing
// ignore_user_abort(true);
// Remove any buffers so that PHP attempts to send data on flush();
while (ob_get_level()){
ob_get_contents();
ob_end_clean();
}
// Let's loop for 10 seconds
// If ignore_user_abort=true:
// This will continue to run regardless.
// If ignore_user_abort=false:
// This will immediate terminate when the user disconnects and PHP tries to flush();
// PHP will begin its shutdown process.
// In either case, connection_aborted() should subsequently return "true" after the user
// has disconnected (hit STOP button in browser), AND after PHP has attempted to flush().
$numSleeps = 0;
while ($numSleeps++ < 10) {
$connAbortedStr = connection_aborted() ? "YES" : "NO";
$str = "Slept $numSleeps times. Connection aborted: $connAbortedStr";
echo "$str<br>";
// If ignore_user_abort = false, script will terminate right here.
// Shutdown functions will being.
// Otherwise, script will continue for all 10 loops and then shutdown.
flush();
$connAbortedStr = connection_aborted() ? "YES" : "NO";
fwrite($fileHandle, "flush()'d $numSleeps times. Connection aborted is now: $connAbortedStr\n");
sleep(1);
}
echo "DONE SLEEPING!<br>";
die;
ignore_user_abort
并查看日志以了解这如何改变事情。
connection_abort
问题的人,
register_shutdown_function
, 和
__destruct
.
关于PHP connection_aborted() 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381270/
我有以下代码: ignore_user_abort(true); while(!connection_aborted()) { // do stuff } 根据PHP文档,这应该一直运行到连接
我正在尝试提供图像,同时为图像被查看的每一秒添加一个 MySQL 行。 我以 1024 位的 block 形式提供它(图像的总大小为 20kb) 问题是,如果我加载显示图像的页面然后关闭窗口或单击将我
我是一名优秀的程序员,十分优秀!