Continue php script after connection close

后端 未结 4 1204
南旧
南旧 2020-12-30 19:09

I am trying to continue a PHP Script after the page/connection is closed.

Users will POLL the script in every 1 hour, I want to return some json output and want to c

4条回答
  •  难免孤独
    2020-12-30 19:17

    After some research i got it work, Sometime it may be useful to some others.

    function closeOutput($stringToOutput){   
            set_time_limit(0);
            ignore_user_abort(true);
            header("Connection: close\r\n");
            header("Content-Encoding: none\r\n");  
            ob_start();          
            echo $stringToOutput;   
            $size = ob_get_length();   
            header("Content-Length: $size",TRUE);  
            ob_end_flush();
            ob_flush();
            flush();   
    } 
    

    You can use it like

    $outputContent = 'Contentent Goes Here...';
    
    closeOutput( $outputContent );
    
    sleep(5);
    
    //do some background works ...
    
    exit();
    

提交回复
热议问题