通过PHP和Apache2同时连接到单个浏览器


Simultaneous connections to single browser through PHP and Apache2

我现在有一个AJAX请求转到sendMail.php,它会立即关闭连接(使用标题connection:Close),并继续处理大约30秒。

但我现在遇到的问题是,当同一个客户端试图从该服务器加载任何PHP页面时,它必须等到sendMail.PHP完成处理。

有办法解决这个问题吗

我读到一些其他SO问题,认为它可能与会话有关,但我没有使用任何会话,我甚至尝试在sendMail.php脚本开始时调用session_write_close()

示例代码(这是一个技巧,做得太多了,但它有效):

   //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调用调用的脚本占用了你在服务器上的可用资源(无论是内存/CPU/Disk I/O等),这阻止了任何新脚本的运行。仔细检查以确保您有足够的资源来完成任务。

顺便说一句,通常情况下,你不允许你的日常用户在服务器上生成运行那么长时间的进程,因为你为他们打开了过载你的系统的机会,并给他们一个容易受到DDOS攻击的机会。如果有可能的话,我会考虑伊贝尔提出的将其转移到cron的建议。