可能重复:
close a connection early
我希望按照以下方式完成某些事情:
foo.html
概念上,我的伪PHP代码可能如下所示:
<?php
//Send content to the user
echo "Hello world!!";
//This terminates the script,
//I simply want to close the
//HTTP part without terminating
exit();
//That only took a few milliseconds
//Send an email to someone
//containing a sum of numbers
//in the Fibonacci sequence
//This task might take minutes to do.
mail(
"foo@example.com",
"Your sum is ready",
fibonacci(100)
);
但是我没有看到一个明确的方法来执行此操作,因为exit()终止了脚本,我没有看到任何方法可以控制HTTP套接字。
我见过close a connection early这是一个有趣的答案,但我希望在没有输出缓冲和刷新的PHP 5.3中实现这一点。
答案 0 :(得分:1)
正如Timbo White在this question中所回答的那样(并且与我实际使用的代码非常相似所以我知道它有效)试试这个:
// buffer all upcoming output
ob_start();
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
// flush all output
ob_end_flush();
ob_flush();
flush();
//now you can do anything down here no matter how long it takes
//because the script appears to have returned to the user.
答案 1 :(得分:0)
查看register_shutdown_function
http://php.net/manual/en/function.register-shutdown-function.php