PHP Ignore_user_abort () function implements client-side disconnect script to continue execution

Source: Internet
Author: User
Ignore_user_abort () can be implemented when the client shuts down can still execute PHP code, can keep the PHP process has been executing, can achieve so-called scheduled task function and continuous process, only need to open the execution script, unless the Apache and other server restart or have script output, This PHP script will always be in the state of execution, but at the cost of a PHP execution of the script's ongoing process, the cost is large, but can achieve a lot of unexpected features.

Define and use the Ignore_user_abort () function to set the execution of the script to terminate if the client disconnects.

This function returns the previous value of the User-abort setting (a Boolean value).

The syntax Ignore_user_abort (setting) parameter description is setting optional. If set to true, disconnecting from the user is ignored and setting to false causes the script to stop running. If this parameter is not set, the current setting is returned. Note: PHP does not detect whether a user has been disconnected until an attempt is made to send a message to the client. Simply use the ECHO statement to not ensure that information is sent, see the flush () function.

Examples of Use:

(1) Implement a cyclic script execution task with Set_time_limit () function

Ignore_user_abort ();

Set_time_limit (0);

$interval =60*15;//instructions: Loop execution every 15 minutes

do{

The business performed

}while (TRUE);

(2) Customizing the implementation file output and tracking the execution results of the ignore_user_abort () function

Ignore_user_abort (TRUE);

Set_time_limit (0);

$interval = 10;

$stop = 1;

do{

if ($stop ==10) break;

File_put_contents (' scutephp.com.php ', ' Current time: '. Time (). ' Stop: '. $stop);

$stop + +;

Sleep ($interval);

}while (TRUE);

Open the scutephp.com.php file with the following file contents: Current time:1273735029 Stop:9 The principle is that even if the client terminates the script, it is still executed every 10 seconds and prints out the present time and end point so that the ignore can be tested The specific effect of the _user_abort () function.

It is also very practical to use the Ignore_user_abort () function to realize the business of planning tasks, continuous processes, and so on.

  • 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.