如何提早关闭连接? [英] How do I close a connection early?
问题描述
我正在尝试进行AJAX调用(通过JQuery),这将启动一个相当长的过程.我希望脚本仅发送一个指示进程已启动的响应,但是JQuery在PHP脚本运行完成之前不会返回响应.
我已经尝试过使用关闭"标头(如下),并且还使用输出缓冲;似乎都不起作用.有什么猜想吗?还是我需要在JQuery中做这件事?
<?php
echo( "We'll email you as soon as this is done." );
header( "Connection: Close" );
// do some stuff that will take a while
mail( 'dude@thatplace.com', "okay I'm done", 'Yup, all done.' );
?>
以下PHP手册页(包括用户注释)建议了有关如何在不结束PHP脚本的情况下关闭与浏览器的TCP连接的多条指令:>
应该比发送关闭标头需要更多的信息.
OP然后确认:是的,这成功了: 在将 解决方案是在发送标题信息之前显式关闭输出缓冲并清除缓冲区.示例: 花了3个小时试图弄清楚这一点,希望它能对某人有所帮助:) 经过以下测试:
2010年7月以后,在相关答案 连接处理用户说明#89177(2009年2月)
I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I'd like the script to simply send a response indicating that the process has started, but JQuery won't return the response until the PHP script is done running. I've tried this with a "close" header (below), and also with output buffering; neither seems to work. Any guesses? or is this something I need to do in JQuery?
The following PHP manual page (incl. user-notes) suggests multiple instructions on how to close the TCP connection to the browser without ending the PHP script: Supposedly it requires a bit more than sending a close header. OP then confirms: yup, this did the trick: pointing to user-note #71172 (Nov 2006) copied here: Closing the users browser connection whilst keeping your php script running has been an issue since [PHP] 4.1, when the behaviour of sts at mail dot xubion dot hu Posted the original solution: Which works fine until you substitute The solution is to explicitly turn off output buffering and clear the buffer prior to sending your header information. Example: Just spent 3 hours trying to figure this one out, hope it helps someone :) Tested in:
Later on in July 2010 in a related answer Arctic Fire then linked two further user-notes that were-follow-ups to the one above: 这篇关于如何提早关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!phpinfo()
替换为echo('text I want user to see');
之前,这种方法都可以正常工作,在这种情况下,永远不会发送标头!<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>
<?php
echo( "We'll email you as soon as this is done." );
header( "Connection: Close" );
// do some stuff that will take a while
mail( 'dude@thatplace.com', "okay I'm done", 'Yup, all done.' );
?>
register_shutdown_function()
was modified so that it would not automatically close the users connection.<?php
header("Connection: close");
ob_start();
phpinfo();
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
sleep(13);
error_log("do something in the background");
?>
phpinfo()
for echo('text I want user to see');
in which case the headers are never sent!<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>