Home >Backend Development >PHP Problem >php returns directly without waiting for the result

php returns directly without waiting for the result

angryTom
angryTomOriginal
2019-10-29 16:39:554198browse

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(&#39;fastcgi_finish_request&#39;)){
    // 刷新buffer
    ob_flush();
    flush();
    // 断开浏览器连接
    fastcgi_finish_request();
}
// 后台继续执行任务
sleep(2);
file_put_contents(&#39;/tmp/test.log&#39;, &#39;ok&#39;);

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more