Home >Backend Development >PHP Problem >php returns directly without waiting for the result
php returns directly without waiting for the result
Sometimes we request a php just to trigger an event, but not Regardless of the execution time and results, you need to immediately return a message to the browser and disconnect, for example, return: The task has started! How to achieve this? You can use fastcgi_finish_request() to disconnect from the browser. The specific implementation is as follows:
<?php echo "这个是输出到浏览器的内容"; // =======这部分是将输出内容刷新到用户浏览器并断开和浏览器的连接===== // 如果使用的是php-fpm if(function_exists('fastcgi_finish_request')){ // 刷新buffer ob_flush(); flush(); // 断开浏览器连接 fastcgi_finish_request(); } // 后台继续执行任务 sleep(2); file_put_contents('/tmp/test.log', 'ok');
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of php returns directly without waiting for the result. For more information, please follow other related articles on the PHP Chinese website!