Why does PHP still run after the browser is closed?

Source: Internet
Author: User
Tags file info
{Code...} after I closed the browser, why did I find that file a is still being written? I deleted file a and will still generate it again. Isn't it true that PHP will terminate the script by default after the connection is closed?
127.0.0.1/a.phpset_time_limit(0);while (true) {    file_put_contents('./a', "data\r\n", FILE_APPEND);    sleep(1);}

Why do I find that file a is still being written after I close the browser? After I delete file a, the file will still be generated. Isn't it true that PHP will terminate the script by default after the connection is closed?

Reply content:
127.0.0.1/a.phpset_time_limit(0);while (true) {    file_put_contents('./a', "data\r\n", FILE_APPEND);    sleep(1);}

Why do I find that file a is still being written after I close the browser? After I delete file a, the file will still be generated. Isn't it true that PHP will terminate the script by default after the connection is closed?

But the premise is that youignore_user_abort()Whether the function is setfalse, IftrueWhen the user closes the browser, the php page will continue to be executed! We also found that you have setset_time_limit(0),That is to say, the script will not time out! Only adjustignore_user_abort()Okay.

The browser initiates a request, and the server starts to execute it until the server completes running or suffers an exception or TIMEOUT. The browser cannot close the server.
But ......
The technology is always not quite developed. rfc http 1.1 defines the Connection response header. For details, refer to PHP Connection processing.
The http stateless protocol is a historical issue. The current HTTP version is also solving this problem, such as the long link mentioned above. Define the Connection in RFC2616 (HTTP 1.1.

Https://segmentfault.com/q/1010000000627...

Yesnginx + phpOrapache + php, Or otherwebserver + php

nginx + phpStrictly speakingphpfpm + phpThis is indeed the case.

When PHP sends a message to the client, it can detect whether the connection has been interrupted.

Http://php.net/manual/zh/function.ignore...
In php. ini, the default value of ignore_user_abort is Off, and user interruption is not ignored. That is, after the client is disconnected, the script is aborted.
Before PHP tries to send the information to the client, it does not detect whether the user has interrupted the connection.
Only the echo statement cannot ensure that the information has been sent. For more information, see flush () function.

Info. php


  

Test:
Time curl http: // 127.0.0.1/info. php
Ctrl+c中 (php-fpm is the last generated file info.txt.
Because the request has no output, PHP cannot detect whether the user has interrupted the connection.

When PHP sends a message to the browser, it can detect whether the connection has been interrupted.
For example, if the code is executed and the request is interrupted within 5 seconds, the info.txt file will not be generated:

for($i=0;$i<5;$i++) { echo $i; ob_flush(); flush(); sleep(1);}file_put_contents('/tmp/info.txt', $i);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.