Home  >  Article  >  Backend Development  >  How to continue processing after PHP access ends

How to continue processing after PHP access ends

(*-*)浩
(*-*)浩Original
2019-10-16 11:32:472220browse

Today I saw someone in dewen asking how to use php to continue executing the subsequent code after the browser access is completed, I wrote a demo, it is very easy to implement in the php-fpm environment, fastcgi_finish_request is enough. If it is another container, I think the jump can only be achieved by outputting javascript to the client, and then continuing execution in the background.

How to continue processing after PHP access ends

The demo is as follows, php-fpm test is available, apache php-cgi is not tested because there is no environment. (Recommended learning: PHP video tutorial)

<?php
// 你要跳转的url
$url = "http://www.baidu.com/";
 
// 如果使用的是php-fpm
if(function_exists(&#39;fastcgi_finish_request&#39;)){
    header("Location: $url");
    ob_flush();
    flush();
    fastcgi_finish_request();
// Apache ?
}else{
    header( &#39;Content-type: text/html; charset=utf-8&#39; );
    if(function_exists(&#39;apache_setenv&#39;))apache_setenv(&#39;no-gzip&#39;, &#39;1&#39;);
    ini_set(&#39;zlib.output_compression&#39;, 0);
    ini_set(&#39;implicit_flush&#39;, 1);
    echo "<script>location=&#39;$url&#39;</script>";
    ob_flush();
    flush();
}
 
    // 这里是模拟你的耗时逻辑
    sleep(2);
    file_put_contents(&#39;/tmp/test.log&#39;, &#39;ok&#39;);

The above is the detailed content of How to continue processing after PHP access ends. 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