如何提前关闭连接? [英] How do I close a connection early?
问题描述
我正在尝试执行一个 AJAX 调用(通过 JQuery),这将启动一个相当长的过程.我希望脚本简单地发送一个响应,表明进程已经开始,但在 PHP 脚本运行完成之前,JQuery 不会返回响应.
我已经尝试过使用关闭"标题(如下)以及输出缓冲;两者似乎都不起作用.有什么猜测吗?或者这是我需要在 JQuery 中做的事情?
以下 PHP 手册页(包括用户注释)建议了有关如何在不结束 PHP 脚本的情况下关闭与浏览器的 TCP 连接的多个说明:
>
据说它需要的不仅仅是发送一个关闭标头.
<小时>OP 然后确认:是的,这成功了: 指向用户注释 #71172(2006 年 11 月) 复制到此处:
<块引用>关闭用户浏览器连接同时保持你的 php 脚本运行一直是一个问题,因为 [PHP] 4.1,当 register_shutdown_function()
的行为被修改时,它不会自动关闭用户连接.
sts at mail dot xubion dot hu 贴出原解:
在您将 phpinfo()
替换为 echo('text I want user to see');
之前,它可以正常工作,在这种情况下,永远不会发送标头!>
解决方案是在发送标头信息之前明确关闭输出缓冲并清除缓冲区.示例:
刚刚花了 3 个小时试图解决这个问题,希望它可以帮助某人 :)
测试:
- IE 7.5730.11
- Mozilla Firefox 1.81
<小时>
稍后在 2010 年 7 月的相关答案 Arctic Fire 然后将另外两个用户注释链接到上述注释:
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?
<?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.' );
?>
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
register_shutdown_function()
was modified so that it would not automatically close the users connection.sts at mail dot xubion dot hu Posted the original solution:
<?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"); ?>
Which works fine until you substitute
phpinfo()
forecho('text I want user to see');
in which case the headers are never sent!The solution is to explicitly turn off output buffering and clear the buffer prior to sending your header information. Example:
<?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'); ?>
Just spent 3 hours trying to figure this one out, hope it helps someone :)
Tested in:
- IE 7.5730.11
- Mozilla Firefox 1.81
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屋!