PHP asynchronous call background call continuous execution disconnect/Browser

Source: Internet
Author: User

Brief Introduction: This is a detailed page for PHP asynchronous call background calls to continuously execute disconnected/browser. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 336871 'rolling = 'no'>

The title is very strange, because I do not know how to use a short sentence to summarize the following content. I just hope that PHP developers will find this introduction more easily through search engines.

The core content of this time is centered around the ignore_user_abort function.

All Web developers know that browsers and servers communicate over HTTP. This is a request and response model-based protocol. URL
Initiate a request to a server. The web server receives the request and runsProgramAnd then make a response (response), that is, a string that matches the HTTP
The Protocol format, which consists of head and body.

One problem is that the Web
The server may execute a program in several milliseconds or minutes. If the program runs slowly and the user may not be patient, the browser will be closed. At this time, the server will receive a notification that the connection status changes, because
HTTP is the top-layer protocol, and there is a layer of TCP below, TCP will know that the connection is interrupted. If the connection is disconnected, the Program on the server will immediately stop running.

Now the key to the problem is that the program on the server immediately stops execution. If the program only reads information, it is okay to say that it will not be read when it stops. What if it is a written program? For example, if a user submits a piece of text, the design of the server may be complicated and several tables need to be updated at the same time. However, for some reason, for example, one of the tables is locked by other processes, then this program will remain waiting. At this time, if the user closes the browser, the program will not wait and quit directly. The result is that the information is incomplete.

For example, the text submitted by the user needs to be written to the tables A, B, C, and D. Writing to tables A and B may be completed in 0.1 milliseconds, but C
If the table is locked by another process, the current process will have to wait. When the user closes the browser and the current process exits, a situation will occur, new Contents exist in tables A and B, while C and D
There is no such content in the table, causing serious data consistency problems!

Although database transactions can be used to roll back the status, the result is that the user's current commit is invalid.

I hope that as long as the user submits the file, it will be saved successfully. It may take too long to execute the file, but it does not matter if the user closes the browser.

So I found the ignore_user_abort function.

When ignore_user_abort (1) is called, the flag of ignoring the user exit is set, that is, the program must be executed to the end, unless it is called in the program.
Exit.

In fact, there is another function, register_shutdown_function, which can register a function or method and call it when the program exits, which is similar
The onUnload and onbeforeunload events in JavaScript.

I have referenced the Chinese PHP manual here.

Related content:

Chapter 40. Connection handling
Note: The following content applies to PhP 3.0.7 and later.

In PHP, the system maintains the connection status. There are three possible states:

0-normal (normal)

1-aborted (exit unexpectedly)

2-Timeout (timeout)

When the PHP script runs normally, the connection is valid. Aborted
Status flag will be opened. The interruption of remote client connection is usually caused by the user clicking the stop button. When the connection time exceeds the PHP time limit (see set_time_limit ()
Function.

You can determine whether the script needs to exit when the client terminates the connection. Sometimes it is much more convenient to run the script completely, even if the Script output is not accepted by a remote browser. By default, the script will exit when the remote client connection is interrupted. This process can be
Ignore_user_abort of PHP. ini or "php_value" corresponding to the Apache. conf setting
Ignore_user_abort "and ignore_user_abort () functions. If PHP is not told to ignore user interruption, the script will be interrupted unless
Register_shutdown_function () sets to disable the trigger function. When a remote user clicks stop
When the script tries to output data again, PHP will detect that the connection has been interrupted and call to close the trigger function.

The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. You can set this value by setting max_execution_time or
The parameter "php_value max_execution_time" corresponding to the Apache. conf setting or set_time_limit ()
Function. When the counter times out, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call
Connection_status () function to check whether timeout causes the function to be called. If timeout causes function call to be disabled, the function returns 2.

Note that the aborted and timeout statuses are valid at the same time. It is possible to tell PHP to ignore the user's exit operation. PHP
Note that the connection has been interrupted but the script is still running. If the running time limit is reached, the script will be exited, And the set function to close and trigger will also be executed. Function
Connection_status () returns 3.

The PHP official website's online documentation contains the original English version. Some user feedback and valuable information are provided below. Here is the link:

Http://www.php.net/manual/en/features.connection-handling.php

From: http://www.leakon.com/archives/22

More articles about "php asynchronous call of background calls to continuously execute disconnected/Browser"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/336871.html pageno: 9.

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.