


PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial
PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background
In the past two days, I have developed a PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial program that calls SVN to synchronize update updates on multiple servers. In order to avoid being blocked during commit for a long time, I have to find a way to only request the trigger without waiting for the program update to complete and return the results. This is so time-consuming. It took too long, so I studied how to make PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP actively disconnects under Nginx Connection Close and ignore_user_abort run in the background_PHP tutorial tutorial actively disconnect. After working on it all afternoon, I found a lot of problems. Fortunately, I finally solved them. The main reason was that Nginx was too messy. .
Without further ado, here’s the code:
<code class="hljs" java="">/** * 主动断开与客户端浏览器的连接 * 如果是 Nginx 服务器需要输出大于等于 fastcgi_buffer_size 缓存的数据才能即时输出 header 断开连接, 若还是不行可尝试关闭 gzip * 如: fastcgi_buffer_size 64k; 即: 需要 64*1024 字符(可多不可少), * 可使用 str_repeat(' ', 65536); 另外 str_repeat(' ', 6554); 这种方式其实生成速度更慢 * @param null|string $str 当前输出的内容, 若无需输出则设置为空 */ public function connectionClose($str = null) { $str = ob_get_contents() . $str; // 若实际输出内容长度小于该值将可能导致主动断开失败 header('Content-Length: '. strlen($str)); Header::connectionClose(); ob_start(); echo $str; ob_flush(); flush(); }</code>
Additional explanation:
There is generally no problem with apache. I used xampp on Windows to debug and found no problems at first. It turned out that it was Nginx on the server. It was dead or alive and crashed for an afternoon. It was later revealed that it was Nginx's fastcgi_buffer. question.
After testing N times under various conditions, there should be no bugs. . .
In addition, let’s talk about the problem of ignore_user_abort() function
When the browser is closed, determine whether the program will continue to execute in the background. (In the example below, you do not have to set it to never timeout limit 0 when testing, just set it for one or two minutes, otherwise It may take a long time to restart the HTTP service)
To put it simply, if you want the program to continue executing after the user's browser is closed, then you must add the following code:
<code bash="" class="hljs">ignore_user_abort(true);</code>
But it is slightly different depending on the situation of your subsequent program (mainly the while loop):
Generally in the program you can monitor the connection status for control:
<code bash="" class="hljs">$isAborted = connection_aborted(); $status = connection_status(); if (0 !== $status || $isAborted) { break; }</code>
But there is a prerequisite for these two functions to work properly, that is, your program must have output content and be larger than the output cache of the current WebServer, so that it will work.
If you simply output a space echo ' '; it may have to loop thousands of times before it is judged, so in order to detect the status more immediately, you must output enough content each time it loops to trigger the status detection. .
So we often encounter a problem here: when the browser is disconnected, even if ignore_user_abort(true); is not used, the program will continue to execute because there is no output, and the infinite loop will continue to run. If it is set It's okay if it times out, otherwise you're really dead.
Post the test code below (the picture is posted mainly to prevent theft hehe~)
<code bash="" class="hljs">set_time_limit(0); ignore_user_abort(true); while (1) { echo str_repeat(' ', 65536); $isAborted = connection_aborted(); $status = connection_status(); file_put_contents('test.txt', 'time: '. time() .'; abroted:'. $isAborted .'; status: '. $status); if (0 !== $status || $isAborted) { break; } sleep(2); }</code>
You can try commenting out this sentence
// echo str_repeat(’ ‘, 65536);
Also
set_time_limit(0); It is best not to use 0

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The solution to the Discuz background login problem is revealed. Specific code examples are needed. With the rapid development of the Internet, website construction has become more and more common, and Discuz, as a commonly used forum website building system, has been favored by many webmasters. However, precisely because of its powerful functions, sometimes we encounter some problems when using Discuz, such as background login problems. Today, we will reveal the solution to the Discuz background login problem and provide specific code examples. We hope to help those in need.

Are you worried about WordPress backend garbled code? Try these solutions, specific code examples are required. With the widespread application of WordPress in website construction, many users may encounter the problem of garbled code in the WordPress backend. This kind of problem will cause the background management interface to display garbled characters, causing great trouble to users. This article will introduce some common solutions to help users solve the trouble of garbled characters in the WordPress backend. Modify the wp-config.php file and open wp-config.

Title: Discuz background account login exception, how to deal with it? When you use the backend management of the Discuz forum system, you may sometimes encounter abnormal account login. This could be due to a variety of reasons, including a wrong password, account being blocked, network connection issues, etc. When encountering this situation, we need to solve the problem through simple troubleshooting and processing. Check whether the account number and password are correct: First, confirm whether the account number and password you entered are correct. When logging in, make sure the capitalization is correct and the password is

How to disable software from running in the background in win11? We are using some software. When we are not using it, we will close the software. Some software will still run in the background after it is closed. During the process of running in the background, the computer will cause a certain amount of lag. Some friends want to know what should be done. How to disable software from running in the background in win11. The editor below has compiled the steps to disable software from running in the background in Win11. If you are interested, follow the editor and take a look below! Steps to disable software running in the background in win11: 1. Press the shortcut key "win+X" and select "Settings" from the options given above. 2. After entering the new interface, click "Apps" and then find "Applications and Features" on the right. 3. In it, find “Microsoft Information” and click

Discuz background login failed? Teach you how to solve it easily! As Discuz, as a popular forum platform, is widely used in website construction and management, sometimes you will encounter backend login failures, which is troubling. Today we will discuss the issues that may cause Discuz backend login failure, provide some solutions, and attach specific code examples. I hope this article can help webmasters and developers who encounter similar problems. 1. Troubleshooting is to solve the problem of Discuz background login failure.

Switching between different applications is achieved through the front and back switching of processes. Background: After Linux starts a program, it switches to the background for execution and wants to continue operating in Linux. In Linux, you can use the following methods to start and exit a program in the background, but still keep its process running: 1. Linux starts a program to execute in the background 1. Use nohup and &: $nohupyour_program& Use the nohup command to make The program ignores the hang-up signal (SIGHUP) so that the program continues to run even if you exit the terminal. The & symbol causes the program to run in the background. 2. Use ctrl+Z: If you have started the program in the foreground, you can use ct

How to correctly use nohup for background task processing In daily work, we often need to perform some time-consuming tasks, such as file copying, data processing, etc. In order not to affect our work efficiency and ensure that tasks can run stably in the background, we can use the nohup command to start these tasks. This article will introduce how to correctly use nohup for background task processing. What is nohup command? nohup is a command in Unix and Unix-like operating systems that is used to run commands or scripts in the background.

ThinkPHP6 backend management system development: Implementing backend functions Introduction: With the continuous development of Internet technology and market demand, more and more enterprises and organizations need an efficient, safe, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query. Environment preparation Before starting, we need to install PHP, MySQL, Com
