How do I close a connection early?

前端 未结 19 1883
情书的邮戳
情书的邮戳 2020-11-22 04:24

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 star

19条回答
  •  生来不讨喜
    2020-11-22 05:09

    this worked for me

    //avoid apache to kill the php running
    ignore_user_abort(true);
    //start buffer output
    ob_start();
    
    echo "show something to user1";
    //close session file on server side to avoid blocking other requests
    session_write_close();
    
    //send length header
    header("Content-Length: ".ob_get_length());
    header("Connection: close");
    //really send content, can't change the order:
    //1.ob buffer to normal buffer,
    //2.normal buffer to output
    ob_end_flush();
    flush();
    //continue do something on server side
    ob_start();
    //replace it with the background task
    sleep(20);
    

提交回复
热议问题