How to continue processing after PHP access ends

(*-*)浩
Release: 2023-02-26 20:26:01
Original
2213 people have browsed it

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;);
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template