2
我目前有一个AJAX请求发送到sendMail.php,它立即关闭连接(使用标头连接:关闭)并继续处理大约30秒。通过PHP和Apache2同时连接到单个浏览器
但我现在遇到的问题是,当同一客户端试图从该服务器加载任何PHP页面时,它必须等到sendMail.php完成处理。
有没有办法解决这个问题?
我正在读一些其他SO问题,它可能是会话相关的,但我没有使用任何会话,我甚至尝试调用session_write_close()在sendMail.php脚本的开始。
实施例的代码(这是哈克并经完成,但它的工作原理):
//File: SendMail.php
//error_reporting(E_ALL);
error_reporting(0);
session_write_close();
set_time_limit(0);
ignore_user_abort(1);
ignore_user_abort(true);
ini_set('ignore_user_abort','1');
apache_setenv('no-gzip', 1);
apache_setenv('KeepAlive',0);
ini_set('zlib.output_compression', 0);
ini_set('output_buffering', 0);
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: Close');
// flush all output
ignore_user_abort(true);
ob_end_flush();
ob_flush();
flush();
sleep(30);//The real code has more stuff, but just for example lets just say it sleeps for 30 seconds
引用的材料的其余部分是通过GET正常导航。
AJAX是异步的(这是第一个A代表的),所以它不应该阻止浏览器的其他活动。 – Barmar
AJAX成功完成(响应码4,http 200 ok)。当连接强制关闭到浏览器时,PHP脚本会继续,但是来自该浏览器的其他输入响应(即导航到下一页)将延迟到第一个PHP脚本(sendMail.php)停止。问题在于该特定脚本需要很长时间,并且使用AJAX和强制关闭HTTP循环访问,但是PHP停止处理该用户,直到初始AJAX请求实际完成为止 – Mattisdada
您可以提供一些示例代码吗? – shapeshifter