close a connection early

2018-12-31 07:19发布

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.' );

?>

标签: php jquery ajax
19条回答
荒废的爱情
2楼-- · 2018-12-31 07:57

A better solution is to fork a background process. It is fairly straight forward on unix/linux:

<?php
echo "We'll email you as soon as this is done.";
system("php somestuff.php dude@thatplace.com >/dev/null &");
?>

You should look at this question for better examples:

PHP execute a background process

查看更多
登录 后发表回答