Home > Article > Backend Development > How to continue processing after PHP access ends
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.
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('fastcgi_finish_request')){ header("Location: $url"); ob_flush(); flush(); fastcgi_finish_request(); // Apache ? }else{ header( 'Content-type: text/html; charset=utf-8' ); if(function_exists('apache_setenv'))apache_setenv('no-gzip', '1'); ini_set('zlib.output_compression', 0); ini_set('implicit_flush', 1); echo "<script>location='$url'</script>"; ob_flush(); flush(); } // 这里是模拟你的耗时逻辑 sleep(2); file_put_contents('/tmp/test.log', 'ok');
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!