How do I close a connection early?

前端 未结 19 1792
情书的邮戳
情书的邮戳 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:17

    Complete version:

    ignore_user_abort(true);//avoid apache to kill the php running
    ob_start();//start buffer output
    
    echo "show something to user";
    session_write_close();//close session file on server side to avoid blocking other requests
    
    header("Content-Encoding: none");//send header to avoid the browser side to take content as gzip format
    header("Content-Length: ".ob_get_length());//send length header
    header("Connection: close");//or redirect to some url: header('Location: http://www.google.com');
    ob_end_flush();flush();//really send content, can't change the order:1.ob buffer to normal buffer, 2.normal buffer to output
    
    //continue do something on server side
    ob_start();
    sleep(5);//the user won't wait for the 5 seconds
    echo 'for diyism';//user can't see this
    file_put_contents('/tmp/process.log', ob_get_contents());
    ob_end_clean();
    

提交回复
热议问题